栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

gjson是什么?怎么用?

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gjson是什么?怎么用?

What?

gjson实际上是get + json的缩写,用于读取 JSON 串,同样的还有一个 sjson(set + json)库用来设置 JSON 串。

How? 1. Get()&Set()
package main

import (
	"fmt"
	"github.com/tidwall/gjson"
)

func main() {
	json := `{"name":{"first":"han","last":"tuo"},"age":22}`
	lastName := gjson.Get(json, "name.last")
	fmt.Println("last name:", lastName.String())

	age := gjson.Get(json, "age")
	fmt.Println("age:", age.Int())
}

注意:gjson.Get()函数实际上返回的是gjson.Result`类型,我们要调用其相应的方法进行转换对应的类型。如上面的**String()和Int()**方法。

如果是直接打印输出,其实可以省略String( ),fmt包的大部分函数都可以对实现fmt.Stringer接口的类型调用**String()**方法。

2. GetMany():一次性get多个值
const json = `
{
  "name":"dj",
  "age":18,
  "pets": ["cat", "dog"],
  "contact": {
    "phone": "123456789",
    "email": "dj@example.com"
  }
}`

func main() {
  results := gjson.GetMany(json, "name", "age", "pets.#", "contact.phone")
  for _, result := range results { //遍历结果集
    fmt.Println(result)
  }
}

3. Valid():校验json
if !gjson.Valid(json) { //判断一个json字符串是否合法
  fmt.Println("error")
} else {
  fmt.Println("ok")
}
4. json行

gjson提供..语法可以将多行数据看成一个数组,每行数据是一个元素:

const json = `
{"name": "Gilbert", "age": 61}
{"name": "Alexa", "age": 34}
{"name": "May", "age": 57}
{"name": "Deloise", "age": 44}`

func main() {
  fmt.Println(gjson.Get(json, "..#"))  //返回有多少行 JSON 数据
  fmt.Println(gjson.Get(json, "..1"))  //返回第一行,即`{"name": "Gilbert", "age": 61}
  fmt.Println(gjson.Get(json, "..#.name"))
  fmt.Println(gjson.Get(json, `..#(name="May").age`))
}

  • ..#.name:#后再接路径,表示对数组中每个元素读取后面的路径,将读取到的值组成一个新数组返回;..#.name表示读取每一行中的name字段,最终返回["Gilbert","Alexa","May","Deloise"];

  • ..#(name="May").age:括号中的内容(name="May")表示条件,所以该条含义为取name为"May"的行中的age字段。

gjson还提供了遍历 JSON 行的方法:gjson.ForEachLine(),参数为 JSON 串和类型为func(line gjson.Result) bool的回调函数。回调返回false时遍历停止。下面代码读取输出每一行的name字段:

gjson.ForEachLine(json, func(line gjson.Result) bool {
  fmt.Println("name:", gjson.Get(line.String(), "name"))
  return true
})
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号