Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

gjson除了能够灵活解析、检索未知数据结构内容,还能够动态创建和修改数据结构内容。

动态创建

示例1,简单使用

Code Block
languagego
func main() {
	j := gjson.New(nil)
	j.Set("name", "John")
	j.Set("score", 99.5)
	fmt.Printf(
		"Name: %s, Score: %v\n",
		j.Get("name").String(),
		j.Get("score").Float32(),
	)
	fmt.Println(j.MustToJsonString())


	// Output:
	// Name: John, Score: 99.5
	// {"name":"John","score":99.5}
}

示例2,创建数组

Code Block
languagego
func main() {
	j := gjson.New(nil)
	for i := 0; i < 5; i++ {
		j.Set(fmt.Sprintf(`%d.id`, i), i)
		j.Set(fmt.Sprintf(`%d.name`, i), fmt.Sprintf(`student-%d`, i))
	}
	fmt.Println(j.MustToJsonString())


	// Output:
	// [{"id":0,"name":"student-0"},{"id":1,"name":"student-1"},{"id":2,"name":"student-2"},{"id":3,"name":"student-3"},{"id":4,"name":"student-4"}]
}

动态修改

Code Block
languagego
func main() {
	data :=
		`{
    "users" : {
        "count" : 2,
        "list"  : [
            {"name" : "Ming", "score" : 60},
            {"name" : "John", "score" : 59}
        ]
    }
}`
	if j
.GetString("name"), j.GetFloat32("score"), )
, err := gjson.DecodeToJson(data); err != nil {
		panic(err)
	} else {
		j.Set("users.list.1.score", 100)
		fmt.Println("John Score:", j.Get("users.list.1.score").Float32())
		fmt.Println(j.MustToJsonString())
	}
	// Output:
	// John Score: 100
	// {"users":{"count":2,"list":[{"name":"Ming","score":60},{"name":"John","score":100}]}}
}

JSON数据通过gjson包读取后,可以通过Set方法改变内部变量的内容,当然也可以新增/删除内容,当需要删除内容时,设定的值为nil即可。gjson包的数据运行时修改特性非常强大,在该特性的支持下,各种数据结构的编码/解析显得异常的灵活方便。


Panel
titleContent Menu

Table of Contents


gjson除了能够灵活解析、检索未知数据结构内容,还能够动态创建和修改数据结构内容。

动态创建

示例1,简单使用

Code Block
languagego
func main() {
	j := gjson.New(nil)
	j.Set("name", "John")
	j.Set("score", 99.5)
	fmt.Printf(
		"Name: %s, Score: %v\n",
		j.Get("name").String(),
		j.Get("score").Float32(),
	)
	fmt.Println(j.MustToJsonString())


	// Output:
	// Name: John, Score: 99.5
	// {"name":"John","score":99.5}
}

示例2,创建数组

Code Block
languagego
func main() {
	j := gjson.New(nil)
	for i := 0; i < 5; i++ {
		j.Set(fmt.Sprintf(`%d.id`, i), i)
		j.Set(fmt.Sprintf(`%d.name`, i), fmt.Sprintf(`student-%d`, i))
	}
	fmt.Println(j.MustToJsonString())


	// Output:
	// [{"id":0,"name":"student-0"},{"id":1,"name":"student-1"},{"id":2,"name":"student-2"},{"id":3,"name":"student-3"},{"id":4,"name":"student-4"}]
}

动态修改

Code Block
languagego
func main() {
	data :=
		`{
    "users" : {
        "count" : 2,
        "list"  : [
            {"name" : "Ming", "score" : 60},
            {"name" : "John", "score" : 59}
        ]
    }
}`
	if j, err := gjson.DecodeToJson(data); err != nil {
		panic(err)
	} else {
		j.Set("users.list.1.score", 100)
		fmt.Println("John Score:", j.
GetFloat32
Get("users.list.1.score").Float32())
		fmt.Println(j.MustToJsonString())
	}
	// Output:
	// John Score: 100
	// {"users":{"count":2,"list":[{"name":"Ming","score":60},{"name":"John","score":100}]}}
}

JSON数据通过gjson包读取后,可以通过Set方法改变内部变量的内容,当然也可以新增/删除内容,当需要删除内容时,设定的值为nil即可。gjson包的数据运行时修改特性非常强大,在该特性的支持下,各种数据结构的编码/解析显得异常的灵活方便。


Panel
titleContent Menu

Table of Contents