解组未知格式的JSON数据[重复]

问题描述:

This question already has an answer here:

My JSON is in the following format:

{
'Math': 
[
    {'Student1': 100.0, 'timestamp': Timestamp('2017-06-26 15:30:00'), 'Student2': 100.0, 'Student3': 97.058823442402414},
    {'Student1': 93.877550824911907, 'timestamp': Timestamp('2017-06-26 15:31:00'), 'Student2': 100.0, 'Student5': 100.0},
    {'Student8': 100.0, 'timestamp': Timestamp('2017-06-26 15:32:00'), 'Student10': 100.0, 'Student4': 100.0}
],
'English': [
    {'Student1': 100.0, 'timestamp': Timestamp('2017-06-26 15:30:00'), 'Student5': 100.0, 'Student3': 97.058823442402414},
    {'Student1': 93.877550824911907, 'timestamp': Timestamp('2017-06-26 15:31:00'), 'Student2': 100.0, 'Student5': 100.0}, 
    {'Student8': 100.0, 'timestamp': Timestamp('2017-06-26 15:32:00'), 'Student10': 100.0, 'Student4': 100.0}
]
}

The keys are completely unknown to me. All I know is that the JSON will be of the format:

{
SUBJECT1: [{Student_Name1: Grade, Student_Name2: Grade, ... , Student_Name3: Grade, timestamp: Timestamp(...)}],
SUBJECT2: [{Student_Name4: Grade, Student_Name6: Grade, ... , Student_Name5: Grade, timestamp: Timestamp(...)}]
...
SUBJECTN: [{Student_Name1: Grade, Student_Name6: Grade, ... , Student_Name9: Grade, timestamp: Timestamp(...)}]
}

where the subjects, student_names are all unknown and could vary.

I want to unmarshal this into a GoLang struct so I can return it to my front-end as a JSON object. What should my struct look like? This is what I tried, but it didn't work.

type GradeData struct {
    Grades map[string]interface{} `json:"-"`
}
</div>

此问题已经存在 在这里有答案: p>

  • 使用未知字段解组JSON [重复] 7个答案 span> \ n li> ul> div>

    我的JSON格式如下: p>

       {
    'Math':
     [
     {'Student1':100.0,'timestamp':Timestamp('2017-06-26 15:30:00'),'Student2':100.0,'Student3':97.058823442402414  },
     {'Student1':93.877550824911907,'timestamp':Timestamp('2017-06-26 15:31:00'),'Student2':100.0,'Student5':100.0},
     {'Student8'  :100.0,'timestamp':Timestamp('2017-06-26 15:32:00'),'Student10':100.0,'Student4':100.0} 
    ],
    '英语':[
     {' 学生1:100.0,“时间戳”:时​​间戳 amp('2017-06-26 15:30:00'),'Student5':100.0,'Student3':97.058823442402414},
     {'Student1':93.877550824911907,'timestamp':Timestamp('2017-06-26  15:31:00'),'Student2':100.0,'Student5':100.0},
     {'Student8':100.0,'timestamp':Timestamp('2017-06-26 15:32:00'),  'Student10':100.0,'Student4':100.0} 
    ] 
    } 
      code>  pre> 
     
     

    这些键对我来说完全是未知的。 我只知道JSON的格式为: p>

      {
    SUBJECT1:[{Student_Name1:Grade,Student_Name2:Grade,...,Student_Name3:Grade,timestamp  :Timestamp(...)}],
    SUBJECT2:[{Student_Name4:成绩,Student_Name6:成绩,...,Student_Name5:成绩,时间戳:Timestamp(...)}] 
     ... 
    SUBJECTN:[  {Student_Name1:成绩,Student_Name6:成绩,...,Student_Name9:成绩,时间戳:Timestamp(...)}] 
    } 
      code>  pre> 
     
     

    code> subjects code>, student_names code>都是未知的,可能会有所不同。 p>

    我想将其解组为GoLang结构,以便可以将其作为JSON对象返回到我的前端。 我的结构应该是什么样的? 这是我尝试过的方法,但是没有用。 p>

     类型GradeData结构{
     Grades map [string] interface {}`json:“-”`
    } 
      code>  pre> 
       DIV>

  • If you don't know the keys, you can use map[string]interface{} to unmarshal your JSON payload.
  • If you use json:"-" tag for the struct fields, those fields will be ignored during JSON Marshal/Unmarshal.

You can try following options: Go Playground link

Option 1:

var grades map[string]interface{}

err := json.Unmarshal([]byte(jsonString), &grades)
fmt.Println(err)

fmt.Printf("%#v
", grades)

Option 2: if you want have struct

var gradesData GradeData
err := json.Unmarshal([]byte(jsonString), &gradesData.Grades)
fmt.Println(err)

fmt.Printf("%#v
", gradesData)