[英]Parsing nested JSON objects without keys using Gson
我有一个包含以下内容的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
(“”)的形式返回,而当行不存在时以地图的形式返回。 我认为这可能是问题的一部分
使用Map<String, String>
代替Line[] lines
。
(您不需要Class Line
)
它应该工作。
或者,如果您的键是整数Map<Integer, String>
也可以工作
[编辑]
您的json字符串表示对象列表: {..},{..},{..}
您需要使用[]
包装。
因此,工作的json应该是:
[
{
"lines": {
"0": "Hammersmith & City",
"1": "Circle"
},
"id": "233",
"name": "Shepherd's Bush Market"
},
{
"lines": {
"0": "Hammersmith & City",
"1": "Circle"
},
"id": "233",
"name": "Shepherd's Bush Market"
},
{
"lines": {
"0": "Hammersmith & City",
"1": "Circle"
},
"id": "233",
"name": "Shepherd's Bush Market"
}
]
为MyObject
public class MyObject {
public String id;
public String name;
public Map<String,String> lines;
}
主要方法
Gson gson = new Gson();
Type type = new TypeToken<List<MyObject>>(){}.getType();
List<MyObject > objList = gson.fromJson(str, type);
assert(objList != null); // validate not null
for(MyObject obj : objList){
System.out.println("id=" + obj.id + "; name=" + obj.name);
}
输出:
id=233; name=Shepherd's Bush Market
id=233; name=Shepherd's Bush Market
id=233; name=Shepherd's Bush Market
在循环中,您也可以提取Map
我喜欢Maxim的简单解决方案,并喜欢+1。 Bur还有另外一种更复杂的方法。 在Lines
类中,您可以将键写为_0
, _1
class Lines {
private String _0;
private String _1;
//@Override
//public String toString() {
// return "Lines [0=" + _0 + ", 1=" + _1 + "]";
//}
}
并像这样在MyObject
使用它
class MyObject {
private Lines lines;
private String id;
private String name;
//@Override
//public String toString() {
// return "Example [lines=" + lines + ", id=" + id + ", name=" + name + "]";
//}
}
此后,您将必须创建将从数字键中删除_
FieldNamingStrategy
。
class MyNameStrategy implements FieldNamingStrategy {
static Pattern numericPattern = Pattern.compile("_\\d+");
@Override
public String translateName(Field f) {
if (numericPattern.matcher(f.getName()).matches()){
return f.getName().substring(1);
}else{
return f.getName();
}
}
}
要使用此策略,您需要通过GsonBuilder
创建Gson
Gson gson = new GsonBuilder().setFieldNamingStrategy(
new MyNameStrategy()).create();
//...
MyObject[] arr = gson.fromJson(jsonString, MyObject[].class);
System.out.println(Arrays.toString(arr));
您也很正确,您的JSon在"lines" : ""
有问题。 由于您将object
( {...}
内的数据)放置在那里,因此以后无法将格式更改为string
(不是JSon格式的对象)。
因此,如果您可以更改JSon,则应将"lines" : ""
替换为
"lines" : null
"lines" : {}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.