testgjson.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. //
  3. //import (
  4. // "fmt"
  5. //
  6. // "github.com/tidwall/gjson"
  7. //)
  8. //
  9. //var json = `
  10. //{
  11. // "name": {"first": "Tom", "last": "Anderson"},
  12. // "age":37,
  13. // "weight": 4.2,
  14. // "foot": nil,
  15. // "male": true,
  16. // "children": ["Sara","Alex","Jack"],
  17. // "fav.movie": "Deer Hunter",
  18. // "friends": [
  19. // {"first": "Dale", "last": "Murphy", "age": 44},
  20. // {"first": "Roger", "last": "Craig", "age": 68},
  21. // {"first": "Jane", "last": "Murphy", "age": 47}
  22. // ]
  23. //}
  24. //`
  25. //
  26. //func main() {
  27. //
  28. // value := gjson.Get(json, "xxxxx")
  29. // println(value.String(), value.Type)
  30. //
  31. // value = gjson.Get(json, "foot")
  32. // println(value.String(), value.Type)
  33. //
  34. // value = gjson.Get(json, "a?e")
  35. // println(value.String(), value.Type)
  36. //
  37. // value = gjson.Get(json, "male")
  38. // println(value.String(), value.Type)
  39. //
  40. // value = gjson.Get(json, "weight")
  41. // println(value.String(), value.Type)
  42. //
  43. // value = gjson.Get(json, "name.last")
  44. // println(value.Raw, value.Type)
  45. //
  46. // value = gjson.Get(json, "name")
  47. // println(value.String(), value.Type)
  48. //
  49. //
  50. // value = gjson.Get(json, "friends")
  51. // println(value.String(), value.Type)
  52. //
  53. // if gjson.Get(json, "name.la?t").Exists() {
  54. // println("has a last name")
  55. // }else{
  56. // println("has't a last name")
  57. // }
  58. //
  59. // if gjson.Get(json, "name.la?t").Exists() {
  60. // println("has a last name")
  61. // }else{
  62. // println("has't a last name")
  63. // }
  64. //
  65. // m, ok := gjson.Parse(json).Value().(map[string]interface{})
  66. // if !ok {
  67. // // not a map
  68. // }else{
  69. // fmt.Println( m )
  70. // }
  71. //
  72. //var jsonString = `
  73. //[
  74. // {
  75. // "name": "elgs",
  76. // "gender": "m",
  77. // "age": 35,
  78. // "skills": [
  79. // "Golang",
  80. // "Java",
  81. // "C"
  82. // ]
  83. // },
  84. // {
  85. // "name": "enny",
  86. // "gender": "f",
  87. // "age": 36,
  88. // "hobby": null,
  89. // "skills": [
  90. // "IC",
  91. // "Electric design",
  92. // "Verification"
  93. // ]
  94. // },
  95. // {
  96. // "name": "sam",
  97. // "gender": "m",
  98. // "age": 1,
  99. // "hobby": "dancing",
  100. // "skills": [
  101. // "Eating",
  102. // "Sleeping",
  103. // "Crawling"
  104. // ]
  105. // }
  106. //]
  107. //`
  108. // value = gjson.Get(jsonString, `#[name=="sam"]`)
  109. // println(value.String(), value.Type)
  110. //
  111. //}