• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

antonholmquist/jason: Easy-to-use JSON Library for Go

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

antonholmquist/jason

开源软件地址:

https://github.com/antonholmquist/jason

开源编程语言:

Go 100.0%

开源软件介绍:

Jason is an easy-to-use JSON library for Go.

Build Status Godoc license

About

Jason is designed to be convenient for reading arbitrary JSON while still honoring the strictness of the language. Inspired by other libraries and improved to work well for common use cases. It currently focuses on reading JSON data rather than creating it. API Documentation can be found on godoc.org.

Install

go get github.com/antonholmquist/jason

Import

import (
  "github.com/antonholmquist/jason"
)

Data types

The following golang values are used to represent JSON data types. It is consistent with how encoding/json uses primitive types.

  • bool, for JSON booleans
  • json.Number/float64/int64, for JSON numbers
  • string, for JSON strings
  • []*Value, for JSON arrays
  • map[string]*Value, for JSON objects
  • nil for JSON null

Examples

Create from bytes

Create object from bytes. Returns an error if the bytes are not valid JSON.

v, err := jason.NewObjectFromBytes(b)

If the root object is unknown or not an object, use NewValueFromBytes instead. It can then be typecasted using one of the conversion methods provided by the library, for instance Array() or String().

v, err := jason.NewValueFromBytes(b)

Create from a reader (like a http response)

Create value from a io.reader. Returns an error if the string couldn't be parsed.

v, err := jason.NewObjectFromReader(res.Body)

Read values

Reading values is easy. If the key path is invalid or type doesn't match, it will return an error and the default value.

name, err := v.GetString("name")
age, err := v.GetInt64("age")
verified, err := v.GetBoolean("verified")
education, err := v.GetObject("education")
friends, err := v.GetObjectArray("friends")
interests, err := v.GetStringArray("interests")

Read nested values

Reading nested values is easy. If the path is invalid or type doesn't match, it will return the default value and an error.

name, err := v.GetString("person", "name")
age, err := v.GetInt64("person", "age")
verified, err := v.GetBoolean("person", "verified")
education, err := v.GetObject("person", "education")
friends, err := v.GetObjectArray("person", "friends")

Loop through array

Looping through an array is done with GetValueArray() or GetObjectArray(). It returns an error if the value at that keypath is null (or something else than an array).

friends, err := person.GetObjectArray("friends")
for _, friend := range friends {
  name, err := friend.GetString("name")
  age, err := friend.GetNumber("age")
}

Loop through object

Looping through an object is easy. GetObject() returns an error if the value at that keypath is null (or something else than an object).

person, err := person.GetObject("person")
for key, value := range person.Map() {
  ...
}

Sample App

Example project:

package main

import (
  "github.com/antonholmquist/jason"
  "log"
)

func main() {

  exampleJSON := `{
    "name": "Walter White",
    "age": 51,
    "children": [
      "junior",
      "holly"
    ],
    "other": {
      "occupation": "chemist",
      "years": 23
    }
  }`

  v, _ := jason.NewObjectFromBytes([]byte(exampleJSON))

  name, _ := v.GetString("name")
  age, _ := v.GetNumber("age")
  occupation, _ := v.GetString("other", "occupation")
  years, _ := v.GetNumber("other", "years")

  log.Println("age:", age)
  log.Println("name:", name)
  log.Println("occupation:", occupation)
  log.Println("years:", years)

  children, _ := v.GetStringArray("children")
  for i, child := range children {
    log.Printf("child %d: %s", i, child)
  }

  others, _ := v.GetObject("other")

  for _, value := range others.Map() {

    s, sErr := value.String()
    n, nErr := value.Number()

    if sErr == nil {
      log.Println("string value: ", s)
    } else if nErr == nil {
      log.Println("number value: ", n)
    }
  }
}

Documentation

Documentation can be found on godoc:

https://godoc.org/github.com/antonholmquist/jason

Test

To run the project tests:

go test

Compatibility

Go 1.1 and up.

Where does the name come from?

I remembered it from an email one of our projects managers sent a couple of years ago.

"Don't worry. We can handle both XML and Jason"

Author

Anton Holmquist, http://twitter.com/antonholmquist




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap