Featured image of post GOLANG 中复杂 JSON 解析

GOLANG 中复杂 JSON 解析

在使用 golang 的时候, 很烦处理 json 数据, 感觉问题不断啊
so: 记录在此, 以便查看 ~

在 encoding/json 包下有提供的方法: Unmarshal
文档中是这样描述的:

  Unmarshal函数解析json编码的数据并将结果存入v指向的值。
  Unmarshal和Marshal做相反的操作,必要时申请映射、切片或指针,有如下的附加规则:
  要将json数据解码写入一个指针,Unmarshal函数首先处理json数据是json字面值null的情况。此时,函数将指针设为nil;否则,函数将json数据解码写入指针指向的值;如果指针本身是nil,函数会先申请一个值并使指针指向它。
  要将json数据解码写入一个结构体,函数会匹配输入对象的键和Marshal使用的键(结构体字段名或者它的标签指定的键名),优先选择精确的匹配,但也接受大小写不敏感的匹配。
  要将json数据解码写入一个接口类型值,函数会将数据解码为如下类型写入接口:

   Bool                   对应JSON布尔类型
   float64                对应JSON数字类型
   string                 对应JSON字符串类型
   []interface{}          对应JSON数组
   map[string]interface{} 对应JSON对象
   nil                    对应JSON的null

  如果一个JSON值不匹配给出的目标类型,或者如果一个json数字写入目标类型时溢出,Unmarshal函数会跳过该字段并尽量完成其余的解码操作。如果没有出现更加严重的错误,本函数会返回一个描述第一个此类错误的详细信息的UnmarshalTypeError。
  JSON的null值解码为go的接口、指针、切片时会将它们设为nil,因为null在json里一般表示“不存在”。 解码json的null值到其他go类型时,不会造成任何改变,也不会产生错误。
  当解码字符串时,不合法的utf-8或utf-16代理(字符)对不视为错误,而是将非法字符替换为unicode字符U+FFFD。

大多时间开发中会用到解析 json string 到 struct 便与管理和调用

json:

1
2
3
4
5
6
{
  "style": 3,
  "color": "black",
  "width": 300,
  "height": 200
}

go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
type BoxStruct struct{
  Style int
  Color string
  Width float64
  Height float64
}

var box BoxStruct
if err := json.Unmarshal([]byte(jsonStr), &box); err == nil {
  fmt.Println("%+b", box)
}

这个只是简单的 json 与 struct 的转换, 往往在处理的时候, 这个 box 并不是 box, 而是 boxes 只需要其声明为数组即可

1
var boxes []BoxStruct

前段时间开发过程中, 还遇到了一个极为复杂的 json 结构处理, 数据结构大概是这样:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{
  "box-count-6572615": {
    "style": 1,
    "sample": [
      {
        "pic": "filenamebox-count-6572615",
        "title": "yaoyi",
        "entitle": "",
        "describe": "",
        "describePic": null,
        "coordinate": [
          {
            "X": 55.033557046979865,
            "Y": 33.63095238095239,
            "title": "yaoyi",
            "content": "yaoyi"
          }
        ]
      },
      {
        "pic": "filename2box-count-6572615",
        "title": "yaoyi",
        "entitle": "",
        "describe": "",
        "describePic": null,
        "coordinate": [
          {
            "X": 59.06040268456376,
            "Y": 39.88095238095239,
            "title": "yaoyi",
            "content": "yaoyi"
          }
        ]
      }
    ]
  },
  "box-count-6605461": {
    "style": 2,
    "sample": [
      {
        "pic": "filenamebox-count-6605461",
        "title": "yaoyi",
        "entitle": "",
        "describe": "",
        "describePic": "describefilebox-count-6605461",
        "coordinate": [
          {
            "X": 48.821548821548824,
            "Y": 51.78571428571429,
            "title": "yaoyi",
            "content": "yaoyi"
          }
        ]
      }
    ]
  }
}

注意 JSON 的 key 值不是固定值 那么

1
map[string]interfalce{}

是跑不了了, 其结构就为 map[ string: struct{ array[ struct{} ] } ]
将其拆解为独个的 struct 然后组合起来
struct

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
type boxStruct struct{
  Style int `json:"style"`
  Sample []sampleStruct `json:"sample"`
}

type sampleStruct struct{
  Pic string `json:"pic"`
  Title string `json:"title"`
  Entitle string `json:"entitle"`
  Describe string `json:"describe"`
  DescribePic string `json:"describePic"`
  Coordinate []coordinateStruct `json:"coordinate"`
}

type coordinateStruct struct{
  X float64 `json:"X"`
  Y float64 `json:"Y"`
  Title string `json:"title"`
  Content string `json:"content"`
}

boxStruct 就是我们需要的结构体了

go

1
2
3
4
var boxes map[string]boxStruct
if err := json.Unmarshal([]byte(jsonStr), &boxes); err == nil {
  fmt.Println("%+b", boxes)
}

THE END.

from Green