安卓:大解析JSON响应 [英] Android : parsing large json response
问题描述
我使用谷歌的 GSON-2.2.1 JSON解析大型响应库。
我要解析一个JSON响应,其中结构可能会有所不同。
I have to parse a JSON response where structure may vary.
第一情况下,当响应包含多个小组:
First case, when the response contains more than one team:
"Details":{
"Role":"abc",
"Team":[
{
"active":"yes",
"primary":"yes",
"content":"abc"
},
{
"active":"yes",
"primary":"yes",
"content":"xyz"
}
],
第二种情况下,当只有一个团队传递:
Second case, when only one team is passed:
"Details":{
"Role":"abc",
"Team":
{
"active":"yes",
"primary":"yes",
"content":"abc"
}
}
有用于解析我的基类:
class Details {
public String Role;
public ArrayList<PlayerTeams> Team = new ArrayList<PlayerTeams>();
PlayerTeams Team; // when JsonObject
}
class PlayerTeams {
public String active;
public String primary;
public String content;
}
问题是,我无法使用的ArrayList&LT; PlayerTeams方式&gt;
时,我有只是其中之一,它的返回的JSONObject
The problem is that I can not use ArrayList<PlayerTeams>
when I have only one of them and it's returned as JsonObject.
GSON可以识别JSON响应的静态格式。我可以动态跟踪充分反应通过检查团队关键是实例 JsonArray
或的JSONObject
,但将是巨大的如果一个更好的解决方案是可用于
Gson can identify static format of JSON response. I can trace full response dynamically by checking if "Team" key is instance of JsonArray
or JsonObject
but it would be great if a better solution is available for that.
编辑:
如果我的回答更加动感。
Edit : If my response is more dynamic..
"Details":{
"Role":"abc",
"Team":
{
"active":"yes",
"primary":"yes",
"content":"abc"
"Test":
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
}
}
在我编辑的问题,我面对的问题,而我的回应是更加动感.. 团队
和测试
可为 JsonArray
或的JSONObject
..这真的骚扰我,因为有时测试
对象可能阵列时,更多的数据,可能会反对在单个数据,字符串时没有数据。不存在响应不一致性。
In my edited question, I am facing problem while my response is more dynamic..Team
and Test
can be JsonArray
or JsonObject
.. It really harassing me because sometime Test
object may array when more data, may object when single data, string when no data. There is no consistency in response.
推荐答案
您需要一个类型的适配器。该适配器将能够区分哪些格式的即将到来,例如合适的对象了正确的价值观。
You need a type adapter. This adapter would be able to distinguish which format is coming and instance the right object with the right values.
您可以做到这一点:
- 通过创建一个类实现
JsonSerializer&LT实现自己的类型的适配器$ C> JsonSerializer
是只需要在情况下,你需要序列化在这个问题了。 - 注册类型适配器与
GsonBuilder
,如:新GsonBuilder()registerTypeAdapter(新TypeToken&LT;名单&LT;团队及GT;&GT;(){。 } .getType(),新CoupleAdapter())。创建()
清单&LT;团队及GT;&gt;中JsonDeserializer&LT;名单&LT;团队及GT;&GT; ,当然<$ C
- implement your own type adapter by creating a class that implements
JsonSerializer<List<Team>>, JsonDeserializer<List<Team>>
, of courseJsonSerializer
is just needed in case you need to serialize it in that matter too. - Register the type adapter to you
GsonBuilder
like:new GsonBuilder().registerTypeAdapter(new TypeToken<List<Team>>() {}.getType(), new CoupleAdapter()).create()
该Deserialize方法可能如下:
The deserialize method could look like:
public List<Team> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws com.google.gson.JsonParseException {
if (json.isJsonObject()) {
return Collections.singleton(context.deserialize(json, Team.class));
} else {
return context.deserialize(json, typeOfT);
}
}
这篇关于安卓:大解析JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!