【发布时间】:2017-06-27 05:54:51
【问题描述】:
我的 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:"-"`
}
【问题讨论】:
-
有什么不好的?解组到
map[string]interface{}是合适的。从那里您可以遍历键并将值解组到数组中。 -
你的 JSON 数据有效吗?
-
@jeevatkm 谢谢。我的 JSON 中充满了转义字符。
标签: json go struct unmarshalling