mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve autoencode feature for package gview
This commit is contained in:
38
encoding/gjson/gjson_z_example_load_test.go
Normal file
38
encoding/gjson/gjson_z_example_load_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
// 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/debug/gdebug"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
func Example_LoadJson() {
|
||||
jsonFilePath := gfile.Join(gdebug.TestDataPath(), "json", "data1.json")
|
||||
j, _ := gjson.Load(jsonFilePath)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
}
|
||||
|
||||
func Example_LoadXml() {
|
||||
jsonFilePath := gfile.Join(gdebug.TestDataPath(), "xml", "data1.xml")
|
||||
j, _ := gjson.Load(jsonFilePath)
|
||||
fmt.Println(j.Get("doc.name"))
|
||||
fmt.Println(j.Get("doc.score"))
|
||||
}
|
||||
|
||||
func Example_LoadContent() {
|
||||
jsonContent := `{"name":"john", "score":"100"}`
|
||||
j, _ := gjson.LoadContent(jsonContent)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
||||
70
encoding/gjson/gjson_z_example_new_test.go
Normal file
70
encoding/gjson/gjson_z_example_new_test.go
Normal file
@ -0,0 +1,70 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
func Example_NewFromJson() {
|
||||
jsonContent := `{"name":"john", "score":"100"}`
|
||||
j := gjson.New(jsonContent)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
||||
|
||||
func Example_NewFromXml() {
|
||||
jsonContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
|
||||
j := gjson.New(jsonContent)
|
||||
fmt.Println(j.Get("doc.name"))
|
||||
fmt.Println(j.Get("doc.score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
||||
|
||||
func Example_NewFromStruct() {
|
||||
type Me struct {
|
||||
Name string `json:"name"`
|
||||
Score int `json:"score"`
|
||||
}
|
||||
me := Me{
|
||||
Name: "john",
|
||||
Score: 100,
|
||||
}
|
||||
j := gjson.New(me)
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
}
|
||||
|
||||
func Example_NewFromStructWithTag() {
|
||||
type Me struct {
|
||||
Name string `tag:"name"`
|
||||
Score int `tag:"score"`
|
||||
Title string
|
||||
}
|
||||
me := Me{
|
||||
Name: "john",
|
||||
Score: 100,
|
||||
Title: "engineer",
|
||||
}
|
||||
j := gjson.NewWithTag(me, "tag")
|
||||
fmt.Println(j.Get("name"))
|
||||
fmt.Println(j.Get("score"))
|
||||
fmt.Println(j.Get("Title"))
|
||||
// Output:
|
||||
// john
|
||||
// 100
|
||||
// engineer
|
||||
}
|
||||
1
encoding/gjson/testdata/json/data1.json
vendored
Normal file
1
encoding/gjson/testdata/json/data1.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"name":"john", "score":"100"}
|
||||
5
encoding/gjson/testdata/xml/data1.xml
vendored
Normal file
5
encoding/gjson/testdata/xml/data1.xml
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<doc>
|
||||
<name>john</name>
|
||||
<score>100</score>
|
||||
</doc>
|
||||
Reference in New Issue
Block a user