我的JSON采用以下格式:
{
'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}
]
}
钥匙对我来说完全不为人知。我所知道的是JSON的格式为:
{
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(...)}]
}
其中subjects
,student_names
都是未知的,可能会有所不同。
我想将其解组为GoLang结构,因此我可以将其作为JSON对象返回到我的前端。我的结构应该是什么样的?这是我尝试过的,但它没有用。
type GradeData struct {
Grades map[string]interface{} `json:"-"`
}
答案 0 :(得分:1)
map[string]interface{}
解组您的JSON有效负载。json:"-"
字段使用struct
标记,则在JSON Marshal / Unmarshal期间将忽略这些字段。您可以尝试以下选项:Go Playground link
选项1:
var grades map[string]interface{}
err := json.Unmarshal([]byte(jsonString), &grades)
fmt.Println(err)
fmt.Printf("%#v\n", grades)
选项2:如果您想要struct
var gradesData GradeData
err := json.Unmarshal([]byte(jsonString), &gradesData.Grades)
fmt.Println(err)
fmt.Printf("%#v\n", gradesData)