本人写的golang解析json数据的包
官方网站http://www.widuu.com/archives/02/965.html 简介
安装go get github.com/widuu/gojson 使用简介结构 type Js struct { data interface{} } (1) json := `{"from":"en","to":"zh"}` c1 := gojson.Json(json) //&{map[from:en to:zh]} (2) json := `{"from":"en","to":"zh","trans_result":{"src":"today","dst":"u4ecau5929"},"result":["src","today","dst","u4ecau5929"]}` c2 := gojson.Json(json).Get("trans_result").Get("dst") fmt.Println(c2) //&{今天} c2 := gojson.Json(json).Get("from") fmt.Println(c2) //&{en} (3) c2 := gojson.Json(json).Get("from").Tostring() fmt.Println(c2) //en (4) c4 := gojson.Json(json).Getpath("trans_result","src").Tostring() fmt.Println(c4) //today (5) json := `{"from":"en","u4ecau5929"]}` c7 := gojson.Json(json).Get("result").Arrayindex(1) fmt.Println(c7) //src (6) json1 := `{"from":"en","trans_result":[{"src":"today",{"src":"tomorrow","dst":"u660eu5929"}]}` c8 := gojson.Json(json1).Get("trans_result").Getkey("src",1).Tostring() fmt.Println(c8) //则返回trans_result第一组中的src today (7) c9k,c9v := gojson.Json(json1).Get("trans_result").ToArray() fmt.Println(c9k,c9v) //[src dst src dst] [today 今天 tomorrow 明天] c3k,c3v := gojson.Json(json).Getindex(1).ToArray() fmt.Println(c3k,c3v) // [from] [en] (8) json1 := `{"from":"en","dst":"u660eu5929"}]}` c10 := gojson.Json(json1).Getindex(3).Getindex(1).Getindex(1).Get("src").Tostring() fmt.Println(c10) //today (9) c11 := gojson.Json(json).Get("result").StringtoArray() fmt.Println(c11) //[src today dst 今天] (10) gojson.Json(json).Get("result").Type() //[]interface {} github地址:https://github.com/widuu/gojson 欢迎拍砖和提交bug问题 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |