Jaso是一个Go语言用来处理JSON文档的开发包。Jaso的强项是解析JSON而不是生成JSON。
示例代码:
root, err := jaso.NewFromReader(res.Body)root.Get("ame").Strig()root.Get("age").Number()root.Get("verified").Boolea()root.Get("educatio").Object()root.Get("frieds").Array()//读取嵌套内容root.Get("perso", "ame").Strig()root.Get("perso", "age").Number()root.Get("perso", "verified").Boolea()root.Get("perso", "educatio").Object()root.Get("perso", "frieds").Array()//判断数值是否存在root.Has("perso", "ame")root.Get("perso", "ame").Exists()//数值校验root.Get("ame").IsStrig()root.Get("age").IsNumber()root.Get("verified").IsBoolea()root.Get("educatio").IsObject()root.Get("frieds").IsArray()root.Get("frieds").IsNull()//循环for _, fried := rage perso.Get("frieds").Array() { ame := fried.Get("ame").Strig() age := fried.Get("age").Number()}完整例子:
package maiimport ( "github.com/atoholmquist/jaso" "log")fuc mai() { exampleJSON := `{ "ame": "Walter White", "age": 51, "childre": [ "juior", "holly" ], "other": { "occupatio": "chemist", "years": 23 } }j, _ := jaso.NewObjectFromBytes([]byte(exampleJSON)) log.Pritl("ame:", j.Get("ame").Strig()) log.Pritl("age:", j.Get("age").Number()) log.Pritl("occupatio:", j.Get("other", "occupatio").Strig()) log.Pritl("years:", j.Get("other", "years").Number()) for i, child := rage j.Get("childre").Array() { log.Pritf("child %d: %s", i, child.Strig()) }}






评论