GO Online Toolset
home
search
Plugin
To Boss

json to struct
14995  |   |   |  113

Feature Introduction

  1. Supports parsing nested JSON strings, recursively parsing nested objects.
  2. Supports parsing array JSON strings, parsing the first JSON string in the array.
  3. Supports injecting custom tags.
  4. Checks if the JSON string format is correct and outputs the error location.
  5. Supports line-by-line parsing of comments.

Example

Parsing Nested JSON String

{
    "name": "test", // Name
    "age": 10, // Age
    "addr": "Chengdu, Sichuan", // Address
    "cls_info": [ // Classes
        {
            "name": "Basketball Class", // Class Name
            "teacher": "Mr. Zhang" // Teacher
        },
        {
            "name": "Art Class", // Class Name
            "teacher": "Mr. Li" // Teacher
        }
    ]
}

After processing:

type ClsInfo struct {
    Name    string `json:"name"`    // Class Name
    Teacher string `json:"teacher"` // Teacher
}

type GenerateObj struct {
    Name    string    `json:"name"`    // Name
    Age     int       `json:"age"`     // Age
    Addr    string    `json:"addr"`    // Address
    ClsInfo []ClsInfo `json:"cls_info"` // Classes
}

Directly Parsing Array JSON String

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

After processing:

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"`
}