【发布时间】:2013-09-27 18:06:13
【问题描述】:
我有一个包含以下内容的 JSON:
[ {
"lines" : {
"0" : "Hammersmith & City",
"1" : "Circle"
},
"id" : "233",
"name" : "Shepherd's Bush Market"
}, {
"lines" :"",
"id" : "233",
"name" : "Shepherd's Bush Market"
}, {
"lines" : {
"0" : "Hammersmith & City",
"1" : "Circle"
},
"id" : "233",
"name" : "Shepherd's Bush Market"
},
, {
"lines" : "",
"id" : "233",
"name" : "Shepherd's Bush Market"
}]
通常,我可以创建这样的对象
public class MyObject {
public String id;
public String name;
public Line[] lines;
public class Line {
public String key;
public String value;
}
}
Gson 序列化程序会处理解析,但在这种情况下,lines 没有任何键/ID。我曾尝试使用HashMaps 和Maps 而不是内部类,但它不起作用。有没有办法可以使用 Gson 解析它?
更新:
我已将 lines 从 MyObject 更改为 Map<String, String> 并在 JSON 响应中添加了更多行
目前这是我用来解析 JSON 的代码
Type listType = new TypeToken<List<MyObject>>(){}.getType();
List<MyObject> data = getGson().fromJson(str, listType);
Caused by: com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialize json object "" given the type java.util.Map<java.lang.String, java.lang.String>
查看整个 JSON 响应后,似乎lines 在不可用时返回为空的String (""),而在可用时返回为映射。我认为这可能是问题的一部分
【问题讨论】:
标签: java android json parsing gson