golang 解析list json

aries 发表于 2020-02-18 1237 次浏览 标签 : golangjson

疫情赶紧过去吧!

今天前台给传来这么一端数据[{"sort":"1", "title":"", "image":"", "link":""}]

由于,初学golang,在后台解析时遇到点麻烦,记录一下

通过这次的实验,我知道了两种解析list json的方法

1 用golang原生的方法

str := `[{"sort":"1", "title":"", "image":"", "link":""}]`
buf := []byte(str)
var res []interface{}
err := json.Unmarshal(buf, &res)
if err != nil {
	return res, err
}
return res, nil

2 用simplejson库

import (
    "fmt"
  "github.com/bitly/go-simplejson"
  "bytes"
)
func main() {
    str := `[{"sort":"1", "title":"", "image":"", "link":""}]`
    buf :=bytes.NewBuffer([]byte(str))
    js,_:=simplejson.NewFromReader(buf)
    fmt.Println(js.GetIndex(0).Get("sort").String())

}

注意,只能解析标准的json

0条评论

如需评论,请填写表单。
换一个

记住我的信息