【the waiter】announcement:

Json to struct
12763  |   |   |  106

feature introduction

  1. Support parsing nested json strings and parsing nested objects according to recursion
  2. Parsing array json string is supported. The first json string in the array will be parsed.
  3. Support for injection of custom tag
  4. Check whether the format of the json string is correct and the location of the output error

example

parsing nested json strings

{
    "name": "test",
    "age": 10,
    "addr": "Sichuan Chengdu",
    "cls_info": [
        {
            "name": "basketball class",
            "teacher": "Mr. Zhang"
        }
        {
            "name": "Art Class",
            "teacher": "Miss Li"
        }
    ]
}

After treatment

type GenerateStruct struct {
    Name string `json: "name,omitempty"`
    Age int `json: "age,omitempty"`
    Addr string `json: "addr,omitempty"`
    ClsInfo [] ClsInfo `json: "cls_info,omitempty"`
}

type ClsInfo struct {
    Name string `json: "name,omitempty" `
    Teacher string `json: "teacher,omitempty" `
}

parsing array json strings directly

[
    {
        "name": "test",
        "age": 10,
        "addr": "Sichuan Chengdu",
        "cls_info": [
            {
                "name": "basketball class",
                "teacher": "Mr. Zhang"
            }
            {
                "name": "Art Class",
                "teacher": "Miss Li"
            }
        ]
    }
    {
        "name": "test2",
        "age": 10,
        "addr": "Sichuan Chengdu",
        "cls_info": [
            {
                "name": "basketball class",
                "teacher": "Mr. Zhang"
            }
            {
                "name": "Art Class",
                "teacher": "Miss Li"
            }
        ]
    }
]

After treatment

type GenerateStruct struct {
    Name string `json: "name,omitempty"`
    Age int `json: "age,omitempty"`
    Addr string `json: "addr,omitempty"`
    ClsInfo [] ClsInfo `json: "cls_info,omitempty"`
}

type ClsInfo struct {
    Name string `json: "name,omitempty" `
    Teacher string `json: "teacher,omitempty" `
}