【发布时间】:2017-08-23 23:55:53
【问题描述】:
我想在 Android Studio 中使用 Gson 库解析以下 JSON 数据。但是数据是通用的..不知道数据中有什么键(对象)..
在学校对象下 - 有数字 1103 是对象。
在那个对象下我们有 shoolname、shortname、students 再次在学生下-有像2201、2202这样的ID ... 这些对象是动态的,不知道有什么反应..
所以问题是如何使用 Gson 在 android 中解析这个 json 字符串?
或任何其他解决方案都是受欢迎的
{
"schools": {
"home": "1103",
"statelevel": "1348"
},
"school": {
"1103": {
"schoolname": "Indira School",
"nameshort": "ind",
"students": {
"2201": {
"name": "Ritesh",
"isCR": true,
"maths": {
"score": 95,
"lastscore": 86
}
},
"2202": {
"name": "Sanket",
"maths": {
"score": 98,
"lastscore": 90
}
},
"2203": {
"name": "Ajit",
"maths": {
"score": 94,
"lastscore": 87
}
}
}
},
"1348": {
"schoolname": "Patil School",
"nameshort": "pat",
"students": {
"3201": {
"name": "Ravi",
"maths": {
"score": 95,
"lastscore": 86
}
},
"3202": {
"name": "Raj",
"isCR": true,
"maths": {
"score": 98,
"lastscore": 90
}
},
"3203": {
"name": "Ram",
"maths": {
"score": 94,
"lastscore": 87
}
}
}
}
}
}
我参考了How to parse dynamic JSON fields with GSON?.. 但在我的情况下不起作用..我也有内部泛型类。
- 我在https://stackoverflow.com/a/23473650/7790252 找到了解决方案。 实现反序列化器以对学校和学生等类进行建模。
【问题讨论】:
-
@oguzhand : 部分重复..在我的情况下,我必须将整个 json 解析到我的 pojo 类中..如果我在 "school:"->{1103...} ..然后它工作了..但是对于内部 json 再次有学生..它没有工作。