简体   繁体   中英

How to create a struct for kind of scenario

How can we define a struct for this kind of output

{"test": {
"124": {
           "Num1": {
                    "name": [],
                    "age":[],
                    "salary": "23565",
                    "location":[
                    {
                    "city": "loc",
                    "street":2
                    },
                    {
                    "city": "loc",
                    "street":2
                    }
                ]
                },
                 "Num2": {
                    "name": [],
                    "age":[],
                    "salary": "23565",
                    "location":[
                    {
                    "city": "loc",
                    "street":2
                    },
                    {
                    "city": "loc",
                    "street":2
                    }
                ]
                },
                 "Num3": {
                    "name": [],
                    "age":[],
                    "salary": "23565",
                    "location":[
                    {
                    "city": "loc",
                    "street":2
                    },
                    {
                    "city": "loc",
                    "street":2
                    }
                ]
                }
            }
        }
    }

    

The output may vary on input or based on file input. For example, as you can see there json inside "124" can vary based on input. For example : it can be Num1, Num2 , Num3 , ...Num10 or in some case just Num1 and Num2 and in some cases it can be more than Num10 objects something like below {"test": { "124": { "Num1":{...} ,.........., "Num12":{...}}} more or less. Not getting how to make it suitable for all cases, tried with the design having some 10 objects of Num but if it has 15 or 20 objects of Num, in my case Unmarshalling will happen for only 10 objects of Num rest will be left behind if it is more. If someone can help me through this it will be helpfull.

This is the only part that looks structured

{
  "name": [],
  "age": [],
  "salary": "23565",
  "location": [
    {
      "city": "loc",
      "street": 2
    },
    {
      "city": "loc",
      "street": 2
    }
  ]
}

So you can create a lets say ABC struct for above

Then what you have is

map[string]map[string]map[string]ABC

Or if the test and 124 are fixed keys you can make

type XYZ struct {
    Test Test `json:"test"`
}

type Test struct {
     X map[string]ABC
}

But anyway you need to deal with a map :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM