There are many methods for data format conversion, please refer to the API documentation: https://pkg.go.dev/github.com/gogf/gf/v2/encoding/gjson

    It is important to note that there are some Must* conversion methods which ensure conversion to the specified data format, otherwise a panic occurs.

    Let’s look at an example to illustrate.

    1. data :=
    2. `{
    3. "users" : {
    4. "count" : 1,
    5. "array" : ["John", "Ming"]
    6. }
    7. }`
    8. if j, err := gjson.DecodeToJson(data); err != nil {
    9. panic(err)
    10. } else {
    11. fmt.Println("JSON:")
    12. fmt.Println(j.MustToJsonString())
    13. fmt.Println("======================")
    14. fmt.Println("XML:")
    15. fmt.Println(j.MustToXmlString())
    16. fmt.Println("======================")
    17. fmt.Println("YAML:")
    18. fmt.Println(j.MustToYamlString())
    19. fmt.Println("======================")
    20. fmt.Println("TOML:")
    21. fmt.Println(j.MustToTomlString())
    22. }
    23. // Output:
    24. // JSON:
    25. // {"users":{"array":["John","Ming"],"count":1}}
    26. // ======================
    27. // XML:
    28. // <users><array>John</array><array>Ming</array><count>1</count></users>
    29. // ======================
    30. // YAML:
    31. // users:
    32. // array:
    33. // - John
    34. // - Ming
    35. // count: 1
    36. //
    37. // ======================
    38. // TOML:
    39. // [users]
    40. // array = ["John", "Ming"]
    41. // count = 1.0

    gjson supports converting JSON to other common data formats, currently supporting mutual conversion among: JSON, XML, INI, YAML/YML, TOML, PROPERTIES, and Struct data formats.