github.com/gogf/gf@v1.16.9/encoding/gjson/gjson_z_example_conversion_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gjson_test
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/gogf/gf/encoding/gjson"
    12  )
    13  
    14  func Example_conversionNormalFormats() {
    15  	data :=
    16  		`{
    17          "users" : {
    18              "count" : 1,
    19              "array" : ["John", "Ming"]
    20          }
    21      }`
    22  
    23  	if j, err := gjson.DecodeToJson(data); err != nil {
    24  		panic(err)
    25  	} else {
    26  		fmt.Println("JSON:")
    27  		fmt.Println(j.MustToJsonString())
    28  		fmt.Println("======================")
    29  
    30  		fmt.Println("XML:")
    31  		fmt.Println(j.MustToXmlString())
    32  		fmt.Println("======================")
    33  
    34  		fmt.Println("YAML:")
    35  		fmt.Println(j.MustToYamlString())
    36  		fmt.Println("======================")
    37  
    38  		fmt.Println("TOML:")
    39  		fmt.Println(j.MustToTomlString())
    40  	}
    41  
    42  	// Output:
    43  	// JSON:
    44  	// {"users":{"array":["John","Ming"],"count":1}}
    45  	// ======================
    46  	// XML:
    47  	// <users><array>John</array><array>Ming</array><count>1</count></users>
    48  	// ======================
    49  	// YAML:
    50  	// users:
    51  	//     array:
    52  	//         - John
    53  	//         - Ming
    54  	//     count: 1
    55  	//
    56  	// ======================
    57  	// TOML:
    58  	// [users]
    59  	//   array = ["John", "Ming"]
    60  	//   count = 1.0
    61  }
    62  
    63  func Example_conversionGetStruct() {
    64  	data :=
    65  		`{
    66          "users" : {
    67              "count" : 1,
    68              "array" : ["John", "Ming"]
    69          }
    70      }`
    71  	if j, err := gjson.DecodeToJson(data); err != nil {
    72  		panic(err)
    73  	} else {
    74  		type Users struct {
    75  			Count int
    76  			Array []string
    77  		}
    78  		users := new(Users)
    79  		if err := j.GetStruct("users", users); err != nil {
    80  			panic(err)
    81  		}
    82  		fmt.Printf(`%+v`, users)
    83  	}
    84  
    85  	// Output:
    86  	// &{Count:1 Array:[John Ming]}
    87  }
    88  
    89  func Example_conversionToStruct() {
    90  	data :=
    91  		`
    92  	{
    93          "count" : 1,
    94          "array" : ["John", "Ming"]
    95      }`
    96  	if j, err := gjson.DecodeToJson(data); err != nil {
    97  		panic(err)
    98  	} else {
    99  		type Users struct {
   100  			Count int
   101  			Array []string
   102  		}
   103  		users := new(Users)
   104  		if err := j.Struct(users); err != nil {
   105  			panic(err)
   106  		}
   107  		fmt.Printf(`%+v`, users)
   108  	}
   109  
   110  	// Output:
   111  	// &{Count:1 Array:[John Ming]}
   112  }