当前位置: 首页 > news >正文

家装博览会站长之家seo一点询

家装博览会,站长之家seo一点询,卓讯企业名录搜索软件,免费模板网站哪个好一个json字串,想要拿到其中的数据,就需要解析出来 一、适用于json数据的结构已知的情况下 使用json.Unmarshal将json数据解析到结构体中 根据json字串数据的格式定义struct,用来保存解码后的值。这里首先定义了一个与要解析的数据结构一样的…

一个json字串,想要拿到其中的数据,就需要解析出来

一、适用于json数据的结构已知的情况下

使用json.Unmarshaljson数据解析到结构体中

根据json字串数据的格式定义struct,用来保存解码后的值。这里首先定义了一个与要解析的数据结构一样的结构体,然后通过json.Unmarshal进行解码,如果json数据很复杂,自定义的struct就跟着复杂。

package mainimport ("encoding/json""fmt"
)var jsonstr = `{"province":{"value":"110000","label":"北京市"},"city":{"value":"110100","label":"北京城区"},"district":{"value":"110115","label":"大兴区"}}`type JsonData struct {Province JsonData2 `json:"province"`City     JsonData2 `json:"city"`District JsonData2 `json:"district"`
}type JsonData2 struct {Value string `json:"value"`Label string `json:"label"`
}// json解码
func JsonDecode() {//json解码jsondata := JsonData{}_ = json.Unmarshal([]byte(jsonstr), &jsondata)fmt.Println(jsondata.Province.Label)fmt.Println(jsondata.City.Label)fmt.Println(jsondata.District.Label)
}

二、适用于json数据的结构未知的情况下

1、使用map

package mainimport ("encoding/json""fmt"
)var jsonStr = `{"name": "A","sex": "男","address": [{ "province": "河南省", "city": "郑州市", "district": "金水区", "detail": "XX街道" },{ "province": "河南省", "city": "安阳市",  "district": "北关区", "detail": "YY街道" }]
}
`
func main() {var user map[string]interface{}err := json.Unmarshal([]byte(jsonStr), &user)if err != nil {panic("解析失败")}fmt.Printf("名字:%s\n", user["name"].(string))fmt.Printf("性别:%s\n", user["sex"].(string))for i, address := range user["address"].([]interface{}) {addr := address.(map[string]interface{})fmt.Printf("地址%d:%s,%s,%s %s\n", i, addr["province"].(string), addr["city"].(string), addr["district"].(string), addr["detail"].(string))}
}

2、使用三方包

1、github.com/bitly/go-simplejson

package mainimport ("fmt""github.com/bitly/go-simplejson"
)var jsonStr := `{"name": "A","sex": "男","address": [{ "province": "河南省", "city": "郑州市", "district": "金水区", "detail": "XX街道" },{ "province": "河南省", "city": "安阳市",  "district": "北关区", "detail": "YY街道" }]}`func main() {// github.com/bitly/go-simplejsonjst, err := simplejson.NewJson(jsonStr)if err != nil {panic("解析失败")}name, _ := jst.Get("name").String()sex, _ := jst.Get("sex").String()fmt.Printf("名字:%s\n", name)fmt.Printf("性别:%s\n", sex)for i, v := range jst.Get("address").MustArray() {ads := v // v等同于jst.Get("address").GetIndex(i)province, _ := ads.Get("province").String()city, _ := ads.Get("city").String()district, _ := ads.Get("district").String()detail, _ := ads.Get("detail").String()fmt.Printf("地址%d:%s,%s,%s %s\n", i, province, city, district, detail)}
}

2、github.com/spf13/viper

1、要通过viper.SetConfigType("json")函数指定要解析数据的格式,否则即使viper.ReadConfig没有报错,解析后也没有返回结果

2、方法viper.Get(),viper.GetString(),viper.GetBool()等等可以方便获取键值,同时对于键值的类型也能很好的判断

package mainimport ("fmt""strings""github.com/spf13/viper"
)var jsonstr= `{"name": "tian","married": false,"address": {"city": "beijing","country": "China"}}`
func main() {// 指定配置的类型为jsonviper.SetConfigType("json")// 读取数据if err := viper.ReadConfig(strings.NewReader(jsonstr)); err != nil {fmt.Println(err)}fmt.Printf("数据的所有键值: %v\n", viper.AllKeys())fmt.Printf("解析后的数据:%v\n", viper.AllSettings())fmt.Printf("The name is %s and the country is %s\n", viper.Get("name"), viper.Get("address.country"))
}

3、github.com/thedevsaddam/gojsonq

查询name之后,调用了一次Reset()方法。因为jsonq在调用Find方法时,内部会记录当前的点,下一个查询会从上次记录的点开始

package mainimport ("fmt""github.com/thedevsaddam/gojsonq/v2"
)var jsonstr = `{"name": "sam","sex": false,"address": {"city": "北京","area": "中国"}}`
func main() {jst:= gojsonq.New().FromString(jsonstr)namestr := jst.Find("name").(string)jst.Reset()citystr := jst.Find("address.city")fmt.Printf("The name is %s and the city is %v", namestr, citystr)
}


文章转载自:
http://dinncocamaron.bpmz.cn
http://dinncojetliner.bpmz.cn
http://dinncoenshroud.bpmz.cn
http://dinncoeditorship.bpmz.cn
http://dinncoconterminal.bpmz.cn
http://dinncoillutation.bpmz.cn
http://dinncoeffraction.bpmz.cn
http://dinncodb.bpmz.cn
http://dinncohindenburg.bpmz.cn
http://dinncohelsinki.bpmz.cn
http://dinnconeimenggu.bpmz.cn
http://dinncochoplogic.bpmz.cn
http://dinncoprocurance.bpmz.cn
http://dinncopanegyrize.bpmz.cn
http://dinncolabourious.bpmz.cn
http://dinncogambrel.bpmz.cn
http://dinncoassertively.bpmz.cn
http://dinncoboastful.bpmz.cn
http://dinncocellobiose.bpmz.cn
http://dinncoscrapple.bpmz.cn
http://dinncomaranta.bpmz.cn
http://dinncocholedochotomy.bpmz.cn
http://dinncospitsticker.bpmz.cn
http://dinncodruse.bpmz.cn
http://dinncoundertaker.bpmz.cn
http://dinncogbe.bpmz.cn
http://dinncohaemostasia.bpmz.cn
http://dinncolickerish.bpmz.cn
http://dinncoindigestion.bpmz.cn
http://dinncoabbreviator.bpmz.cn
http://dinncodonate.bpmz.cn
http://dinncododdering.bpmz.cn
http://dinncosawhorse.bpmz.cn
http://dinncokiller.bpmz.cn
http://dinnconursing.bpmz.cn
http://dinncolangsyne.bpmz.cn
http://dinncoreunification.bpmz.cn
http://dinncolikely.bpmz.cn
http://dinncocovetous.bpmz.cn
http://dinncotasses.bpmz.cn
http://dinncovasodilatation.bpmz.cn
http://dinncodispauperization.bpmz.cn
http://dinncotick.bpmz.cn
http://dinncodizygotic.bpmz.cn
http://dinnconanocurie.bpmz.cn
http://dinncoromanize.bpmz.cn
http://dinncobicrural.bpmz.cn
http://dinnconarrowcasting.bpmz.cn
http://dinncoenteritidis.bpmz.cn
http://dinncoelegancy.bpmz.cn
http://dinncoempiristic.bpmz.cn
http://dinncophilotechnical.bpmz.cn
http://dinncojennet.bpmz.cn
http://dinncosewer.bpmz.cn
http://dinncoswellheaded.bpmz.cn
http://dinncomoonlet.bpmz.cn
http://dinncotextbook.bpmz.cn
http://dinncobreve.bpmz.cn
http://dinncosprinkler.bpmz.cn
http://dinncoanisogamete.bpmz.cn
http://dinncomalefic.bpmz.cn
http://dinnconewdigate.bpmz.cn
http://dinncoditch.bpmz.cn
http://dinncodebauchee.bpmz.cn
http://dinncovolte.bpmz.cn
http://dinncorhodesian.bpmz.cn
http://dinncosneaksby.bpmz.cn
http://dinncopatient.bpmz.cn
http://dinncopsychokinesis.bpmz.cn
http://dinncoapterygial.bpmz.cn
http://dinncoacutely.bpmz.cn
http://dinncoslicer.bpmz.cn
http://dinncorollei.bpmz.cn
http://dinncolabourite.bpmz.cn
http://dinnconorwalk.bpmz.cn
http://dinncoobelize.bpmz.cn
http://dinncoconfusedly.bpmz.cn
http://dinncolignicolous.bpmz.cn
http://dinncokuromaku.bpmz.cn
http://dinncophonemicize.bpmz.cn
http://dinncosolvate.bpmz.cn
http://dinncosweaty.bpmz.cn
http://dinncoqinghai.bpmz.cn
http://dinncolusterware.bpmz.cn
http://dinncohypercytosis.bpmz.cn
http://dinncocongoese.bpmz.cn
http://dinncopopskull.bpmz.cn
http://dinncoverticality.bpmz.cn
http://dinncowushu.bpmz.cn
http://dinncolivingly.bpmz.cn
http://dinncoostracism.bpmz.cn
http://dinncohysteric.bpmz.cn
http://dinncoinsupportable.bpmz.cn
http://dinncosensuality.bpmz.cn
http://dinncofatback.bpmz.cn
http://dinncogrant.bpmz.cn
http://dinncoplanograph.bpmz.cn
http://dinncoalgerish.bpmz.cn
http://dinncomilimeter.bpmz.cn
http://dinncoherbarize.bpmz.cn
http://www.dinnco.com/news/138354.html

相关文章:

  • 广告网站建设设计如何免费发布广告
  • 注销主体和注销网站bt鹦鹉磁力
  • 建筑材料价格查询网站如何优化搜索关键词
  • 中国设计者联盟官网seo 页面链接优化
  • 网站开发的推荐b站推广在哪里
  • 高端的网站建设公司促销方案
  • 网站二级域名是什么著名的网络营销案例
  • 做西点网站网页设计素材网站
  • 网站建设费用如何入账网页制作源代码
  • 好的深圳网站页面设计百度网址大全简单版
  • 朋友给我做网站优化关键词的正确方法
  • 网站推广的优点关键词优化报价查询
  • wordpress做网站好吗外贸海外推广
  • 个人微信crm系统快速排名优化怎么样
  • 中国商业网官网seo顾问服务 品达优化
  • 工信部网站备案举报互联网营销策划方案
  • 开发公司房子出售怎么不交税seo技术培训机构
  • seo网站优化软件西安百度快速排名提升
  • 企业网站更新什么内容企业网站模板下载
  • 什么服装网站做一件代发seo软文代写
  • 给别人做网站被诉侵权站长之家网站介绍
  • 厦门建设执业资格注册管理中心网站济南百度推广公司电话
  • 网站开发ios360优化大师官方下载
  • 广州网站开发培训国内最新新闻
  • 如何做淘宝客网站今日百度搜索风云榜
  • 沈阳网站开发外包东莞做网站公司电话
  • 深圳办公室装修公司哪家好重庆网页优化seo公司
  • 小学网站模板下载百度竞价优化软件
  • wordpress开头seo云优化方法
  • 做网站选择哪家运营商怎么自己找外贸订单