124 lines
3.8 KiB
Go
124 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"git.kingecg.top/kingecg/gjson"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println("GJson 示例程序")
|
|
|
|
// 创建JSONObject
|
|
obj := gjson.NewJSONObject()
|
|
obj.Put("name", gjson.NewJSONString("张三").Value())
|
|
obj.Put("age", gjson.NewJSONNumber(25).Value())
|
|
obj.Put("student", gjson.NewJSONBool(false).Value())
|
|
|
|
// 创建嵌套对象
|
|
address := gjson.NewJSONObject()
|
|
address.Put("city", gjson.NewJSONString("北京").Value())
|
|
address.Put("zip", gjson.NewJSONNumber(100000).Value())
|
|
obj.Put("address", address.GetData()) // 使用GetData()方法
|
|
|
|
// 创建数组
|
|
arr := gjson.NewJSONArray()
|
|
arr.Append(gjson.NewJSONString("item1").Value())
|
|
arr.Append(gjson.NewJSONNumber(123).Value())
|
|
obj.Put("items", arr.GetData()) // 使用GetData()方法
|
|
|
|
// 序列化到JSON
|
|
jsonStr, _ := obj.ToJSON()
|
|
fmt.Printf("JSON对象: %s\n", jsonStr)
|
|
|
|
// 使用Stringify函数
|
|
stringified, _ := gjson.Stringify(obj)
|
|
fmt.Printf("Stringify结果: %s\n", stringified)
|
|
|
|
// 使用PrettyPrint格式化输出
|
|
pretty, _ := gjson.PrettyPrint(obj)
|
|
fmt.Printf("格式化输出:\n%s\n", pretty)
|
|
|
|
// 使用Compact函数
|
|
compact, _ := gjson.Compact(obj)
|
|
fmt.Printf("紧凑格式: %s\n", compact)
|
|
|
|
// 从JSON字符串创建对象
|
|
newObj := gjson.NewJSONObject()
|
|
newObj.FromJSON(jsonStr)
|
|
|
|
// 获取属性值
|
|
nameVal, _ := newObj.Get("name")
|
|
name := gjson.NewJSONString(fmt.Sprintf("%v", nameVal))
|
|
fmt.Printf("姓名: %s\n", name.Value())
|
|
|
|
// 测试嵌套属性
|
|
addressVal, _ := newObj.Get("address")
|
|
if addrMap, ok := addressVal.(map[string]interface{}); ok {
|
|
cityVal, exists := addrMap["city"]
|
|
if exists {
|
|
fmt.Printf("地址城市: %v\n", cityVal)
|
|
}
|
|
}
|
|
|
|
// 使用GetProperty方法
|
|
defaultValue := gjson.NewJSONString("default")
|
|
propResult, _ := gjson.GetProperty(newObj, "name", defaultValue)
|
|
if str, ok := propResult.(*gjson.JSONString); ok {
|
|
fmt.Printf("通过GetProperty获取的姓名: %s\n", str.Value())
|
|
}
|
|
|
|
propResult2, _ := gjson.GetProperty(newObj, "address.city", defaultValue)
|
|
if str, ok := propResult2.(*gjson.JSONString); ok {
|
|
fmt.Printf("通过GetProperty获取的城市: %s\n", str.Value())
|
|
}
|
|
|
|
// 设置属性值
|
|
gjson.SetProperty(newObj, "address.city", gjson.NewJSONString("上海"))
|
|
modifiedProp, _ := gjson.GetProperty(newObj, "address.city", defaultValue)
|
|
if str, ok := modifiedProp.(*gjson.JSONString); ok {
|
|
fmt.Printf("修改后的城市: %s\n", str.Value())
|
|
}
|
|
|
|
// 创建JSONArray并演示其用法
|
|
jsonArray := gjson.NewJSONArray()
|
|
jsonArray.Append(gjson.NewJSONString("first").Value())
|
|
jsonArray.Append(gjson.NewJSONNumber(42).Value())
|
|
|
|
nestedObj := gjson.NewJSONObject()
|
|
nestedObj.Put("nestedProp", gjson.NewJSONString("nestedValue").Value())
|
|
jsonArray.Append(nestedObj.GetData()) // 使用GetData()方法
|
|
|
|
arrayJson, _ := jsonArray.ToJSON()
|
|
fmt.Printf("JSON数组: %s\n", arrayJson)
|
|
|
|
// 使用Stringify格式化数组
|
|
arrayStringified, _ := gjson.Stringify(jsonArray, " ")
|
|
fmt.Printf("格式化数组:\n%s\n", arrayStringified)
|
|
|
|
// 访问数组元素
|
|
firstElement, _ := jsonArray.Get(0)
|
|
fmt.Printf("第一个元素: %v\n", firstElement)
|
|
|
|
// 访问嵌套对象
|
|
nestedValue, _ := gjson.GetProperty(jsonArray, "2.nestedProp", gjson.NewJSONString("default"))
|
|
if str, ok := nestedValue.(*gjson.JSONString); ok {
|
|
fmt.Printf("嵌套对象属性: %s\n", str.Value())
|
|
}
|
|
|
|
// 演示各种值类型的Stringify
|
|
nullValue := gjson.NewJSONNull()
|
|
nullStr, _ := gjson.Stringify(nullValue)
|
|
fmt.Printf("空值Stringify: %s\n", nullStr)
|
|
|
|
boolValue := gjson.NewJSONBool(true)
|
|
boolStr, _ := gjson.Stringify(boolValue)
|
|
fmt.Printf("布尔值Stringify: %s\n", boolStr)
|
|
|
|
numValue := gjson.NewJSONNumber(42.5)
|
|
numStr, _ := gjson.Stringify(numValue)
|
|
fmt.Printf("数字Stringify: %s\n", numStr)
|
|
|
|
strValue := gjson.NewJSONString("hello world")
|
|
strStr, _ := gjson.Stringify(strValue)
|
|
fmt.Printf("字符串Stringify: %s\n", strStr)
|
|
} |