I have a JSON with this content:
[ {
"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"
}]
Normally, I could create an object like this
public class MyObject {
public String id;
public String name;
public Line[] lines;
public class Line {
public String key;
public String value;
}
}
And the Gson serializer would handle the parsing, but in this case lines doesn't have any keys/ids. I have tried using HashMaps and Maps instead of inner classes, but it doesn't work. Is there a way I can parse this using Gson?
UPDATE:
I have changed lines from MyObject to a Map<String, String> and added some more lines to JSON response
At the moment this is the code I'm using to parse the 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>
After looking through the entire JSON response, it seems that lines is returned as a empty String ("") when it's not available and as a map when it is. I think this may be part of the problem