2020-03-21 21:32:02 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gjson_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
|
|
)
|
|
|
|
|
2020-03-22 12:49:46 +08:00
|
|
|
func Example_conversionNormalFormats() {
|
2020-03-21 21:32:02 +08:00
|
|
|
data :=
|
|
|
|
`{
|
|
|
|
"users" : {
|
|
|
|
"count" : 1,
|
|
|
|
"array" : ["John", "Ming"]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
if j, err := gjson.DecodeToJson(data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("JSON:")
|
|
|
|
fmt.Println(j.MustToJsonString())
|
|
|
|
fmt.Println("======================")
|
|
|
|
|
|
|
|
fmt.Println("XML:")
|
|
|
|
fmt.Println(j.MustToXmlString())
|
|
|
|
fmt.Println("======================")
|
|
|
|
|
|
|
|
fmt.Println("YAML:")
|
|
|
|
fmt.Println(j.MustToYamlString())
|
|
|
|
fmt.Println("======================")
|
|
|
|
|
|
|
|
fmt.Println("TOML:")
|
|
|
|
fmt.Println(j.MustToTomlString())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// JSON:
|
|
|
|
// {"users":{"array":["John","Ming"],"count":1}}
|
|
|
|
// ======================
|
|
|
|
// XML:
|
|
|
|
// <users><array>John</array><array>Ming</array><count>1</count></users>
|
|
|
|
// ======================
|
|
|
|
// YAML:
|
|
|
|
// users:
|
|
|
|
// array:
|
|
|
|
// - John
|
|
|
|
// - Ming
|
|
|
|
// count: 1
|
|
|
|
//
|
|
|
|
// ======================
|
|
|
|
// TOML:
|
|
|
|
// [users]
|
|
|
|
// array = ["John", "Ming"]
|
|
|
|
// count = 1.0
|
|
|
|
}
|
|
|
|
|
2020-03-22 12:49:46 +08:00
|
|
|
func Example_conversionGetStruct() {
|
2020-03-21 21:32:02 +08:00
|
|
|
data :=
|
|
|
|
`{
|
|
|
|
"users" : {
|
|
|
|
"count" : 1,
|
|
|
|
"array" : ["John", "Ming"]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
if j, err := gjson.DecodeToJson(data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
type Users struct {
|
|
|
|
Count int
|
|
|
|
Array []string
|
|
|
|
}
|
|
|
|
users := new(Users)
|
|
|
|
if err := j.GetStruct("users", users); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Printf(`%+v`, users)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// &{Count:1 Array:[John Ming]}
|
|
|
|
}
|
|
|
|
|
2020-03-22 12:49:46 +08:00
|
|
|
func Example_conversionToStruct() {
|
2020-03-21 21:32:02 +08:00
|
|
|
data :=
|
|
|
|
`
|
|
|
|
{
|
|
|
|
"count" : 1,
|
|
|
|
"array" : ["John", "Ming"]
|
|
|
|
}`
|
|
|
|
if j, err := gjson.DecodeToJson(data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
type Users struct {
|
|
|
|
Count int
|
|
|
|
Array []string
|
|
|
|
}
|
|
|
|
users := new(Users)
|
|
|
|
if err := j.ToStruct(users); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Printf(`%+v`, users)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// &{Count:1 Array:[John Ming]}
|
|
|
|
}
|