Jason 基于 Go 的 JSON 开发包开源项目

我要开发同款
匿名用户2014年12月03日
18阅读
所属分类Google Go、Web应用开发、JSON/BSON开发包
授权协议MIT

作品详情

Jason是一个Go语言用来处理JSON文档的开发包。Jason的强项是解析JSON而不是生成JSON。

示例代码:

root, err := jason.NewFromReader(res.Body)root.Get("name").String()root.Get("age").Number()root.Get("verified").Boolean()root.Get("education").Object()root.Get("friends").Array()//读取嵌套内容root.Get("person", "name").String()root.Get("person", "age").Number()root.Get("person", "verified").Boolean()root.Get("person", "education").Object()root.Get("person", "friends").Array()//判断数值是否存在root.Has("person", "name")root.Get("person", "name").Exists()//数值校验root.Get("name").IsString()root.Get("age").IsNumber()root.Get("verified").IsBoolean()root.Get("education").IsObject()root.Get("friends").IsArray()root.Get("friends").IsNull()//循环for _, friend := range person.Get("friends").Array() {  name := friend.Get("name").String()  age := friend.Get("age").Number()}

完整例子:

package mainimport (  "github.com/antonholmquist/jason"  "log")func main() {  exampleJSON := `{    "name": "Walter White",    "age": 51,    "children": [      "junior",      "holly"    ],    "other": {      "occupation": "chemist",      "years": 23    }  }j, _ := jason.NewObjectFromBytes([]byte(exampleJSON))  log.Println("name:", j.Get("name").String())  log.Println("age:", j.Get("age").Number())  log.Println("occupation:", j.Get("other", "occupation").String())  log.Println("years:", j.Get("other", "years").Number())  for i, child := range j.Get("children").Array() {    log.Printf("child %d: %s", i, child.String())  }}
查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论