我有db的json响应。
{"StudentId":"1","SubjectId":"1","Mark":"61"}{"StudentId":"1","SubjectId":"2","Mark":"75"}{"StudentId":"1","SubjectId":"3","Mark":"87"}{"StudentId":"2","SubjectId":"1","Mark":"82"}{"StudentId":"2","SubjectId":"2","Mark":"64"}{"StudentId":"2","SubjectId":"3","Mark":"77"}
我想转换为
{"StudentId":"1",
"Mark":[ "1":"61", "2":"75", "3":"87" ]
}
{"StudentId":"2",
"Mark":[ "1":"82", "2":"64", "3":"77" ]
}
通过使用这个,我想生成一个html表。
答案 0 :(得分:1)
您是否有机会使用Couchbase?在N1QL中,这种转换非常容易。这是查询:
SELECT data.StudentId, OBJECT v.SubjectId:v.Mark FOR v IN ARRAY_AGG(data) END as Mark
FROM [{"StudentId":"1","SubjectId":"1","Mark":"61"},
{"StudentId":"1","SubjectId":"2","Mark":"75"},
{"StudentId":"1","SubjectId":"3","Mark":"87"},
{"StudentId":"2","SubjectId":"1","Mark":"82"},
{"StudentId":"2","SubjectId":"2","Mark":"64"},
{"StudentId":"2","SubjectId":"3","Mark":"77"}] data
GROUP BY data.StudentId
大概您将用查询替换FROM子句中的硬编码数组以首先获取数据。
以下是输出:
[
{
"Mark": {
"1": "61",
"2": "75",
"3": "87"
},
"StudentId": "1"
},
{
"Mark": {
"1": "82",
"2": "64",
"3": "77"
},
"StudentId": "2"
}
]
答案 1 :(得分:0)
{"StudentId":"1","SubjectId":"1","Mark":"61"},
{"StudentId":"1","SubjectId":"2","Mark":"75"},
{"StudentId":"1","SubjectId":"3","Mark":"87"},
{"StudentId":"2","SubjectId":"1","Mark":"82"},
{"StudentId":"2","SubjectId":"2","Mark":"64"},
{"StudentId":"2","SubjectId":"3","Mark":"77"}
在数据库中,我有一个结果表,其属性为StudentId,SubjectId,Mark 我获取了数据并转换为JSON。 我终于想要这种格式的数据。
[
{
"Mark": {
"1": "61",
"2": "75",
"3": "87"
},
"StudentId": "1"
},
{
"Mark": {
"1": "82",
"2": "64",
"3": "77"
},
"StudentId": "2"
}
]