data_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package gmdata
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. )
  7. func TestShopJson(t *testing.T) {
  8. type Item struct {
  9. ID int `json:"ID"`
  10. Materials []struct {
  11. ID int `json:"id"`
  12. Count int `json:"count"`
  13. } `json:"Materials"`
  14. Equipments []int `json:"Equipments"`
  15. Gift interface{} `json:"Gift"`
  16. Cost []struct {
  17. ID int `json:"id"`
  18. Count int `json:"count"`
  19. } `json:"Cost"`
  20. OtherGoods interface{} `json:"OtherGoods"`
  21. AdCoolDown int `json:"AdCoolDown"`
  22. AdDayTimes int `json:"AdDayTimes"`
  23. TotalTimes int `json:"TotalTimes"`
  24. FirstDouble int `json:"FirstDouble"`
  25. GoodsType int `json:"GoodsType"`
  26. StartTime int `json:"StartTime"`
  27. EndTime int `json:"EndTime"`
  28. OpenLevel int `json:"OpenLevel"`
  29. MaxLevel int `json:"MaxLevel"`
  30. }
  31. jsonData := `{
  32. "1": {
  33. "ID": 1,
  34. "Materials": null,
  35. "Equipments": null,
  36. "Gift": null,
  37. "Cost": [{
  38. "id": 1012,
  39. "count": 1
  40. }],
  41. "OtherGoods": null,
  42. "AdCoolDown": 0,
  43. "AdDayTimes": 0,
  44. "TotalTimes": 0,
  45. "FirstDouble": 0,
  46. "GoodsType": 1,
  47. "StartTime": 0,
  48. "EndTime": 0,
  49. "OpenLevel": 0,
  50. "MaxLevel": 1000000
  51. },
  52. "10": {
  53. "ID": 10,
  54. "Materials": [{
  55. "id": 1001,
  56. "count": 20000
  57. },
  58. {
  59. "id": 1002,
  60. "count": 600
  61. },
  62. {
  63. "id": 6005,
  64. "count": 20
  65. }
  66. ],
  67. "Equipments": [50104001],
  68. "Gift": null,
  69. "Cost": [{
  70. "id": 0,
  71. "count": 6
  72. }],
  73. "OtherGoods": null,
  74. "AdCoolDown": 300,
  75. "AdDayTimes": 0,
  76. "TotalTimes": 0,
  77. "FirstDouble": 0,
  78. "GoodsType": 6,
  79. "StartTime": 0,
  80. "EndTime": 0,
  81. "OpenLevel": 0,
  82. "MaxLevel": 1000000
  83. }
  84. }`
  85. var data map[string]Item
  86. err := json.Unmarshal([]byte(jsonData), &data)
  87. if err != nil {
  88. fmt.Println("Unmarshal error:", err)
  89. return
  90. }
  91. // 遍历解析后的数据
  92. for key, value := range data {
  93. fmt.Printf("Key: %s\n", key)
  94. fmt.Printf("ID: %d\n", value.ID)
  95. fmt.Printf("Materials: %v\n", value.Materials)
  96. fmt.Printf("Equipments: %v\n", value.Equipments)
  97. fmt.Printf("Cost: %v\n", value.Cost)
  98. // ... 解析其他字段
  99. fmt.Println("--------------------")
  100. }
  101. }