通过XML数据创建

  1. jsonContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>`
  2. j := gjson.New(jsonContent)
  3. // Note that there's root node in the XML content.
  4. fmt.Println(j.Get("doc.name"))
  5. fmt.Println(j.Get("doc.score"))
  6. // Output:
  7. // 100

自定义Struct转换标签

  1. type Me struct {
  2. Name string `tag:"name"`
  3. Score int `tag:"score"`
  4. Title string
  5. }
  6. me := Me{
  7. Name: "john",
  8. Score: 100,
  9. Title: "engineer",
  10. // The parameter <tags> specifies custom priority tags for struct conversion to map,
  11. // multiple tags joined with char ','.
  12. fmt.Println(j.Get("name"))
  13. fmt.Println(j.Get("score"))
  14. fmt.Println(j.Get("Title"))
  15. // Output:
  16. // john
  17. // 100
  18. // engineer

使用Load*方法创建

    1. jsonFilePath := gdebug.TestDataPath("json", "data1.xml")
    2. j, _ := gjson.Load(jsonFilePath)
    3. fmt.Println(j.Get("doc.name"))

通过LoadContent创建