From 8cb6086f736961c82f7361f65782114d2292d9c8 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sat, 12 Feb 2022 17:27:32 +0800 Subject: [PATCH] Implemented gjson Example 1. New 2.NewWithTag 3.NewWithOptions --- encoding/gjson/gjson_z_example_new_test.go | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index 644e567c4..a077f280c 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -72,3 +72,61 @@ func Example_newFromStructWithTag() { // 100 // engineer } + +func ExampleNew() { + jsonContent := `{"name":"john", "score":"100"}` + j := gjson.New(jsonContent) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleNewWithTag() { + 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 +} + +func ExampleNewWithOptions() { + type Me struct { + Name string `tag:"name"` + Score int `tag:"score"` + Title string + } + me := Me{ + Name: "john", + Score: 100, + Title: "engineer", + } + + j := gjson.NewWithOptions(me, gjson.Options{ + Tags: "tag", + }) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + fmt.Println(j.Get("Title")) + + // Output: + // john + // 100 + // engineer +}