How can I parse json using gson? I have a json array with multiple object types, and I don't know, what kind of object I need to create to save this structure. I cannot change the json data format (I don't control the server). Can I use gson or other library parse this json array, how should I do?
This is the json code block:
[
{
"type": 1,
"object": {
"title1": "title1",
"title2": "title2"
}
},
{
"type": 2,
"object": [
"string",
"string",
"string"
]
},
{
"type": 3,
"object": [
{
"url": "url",
"text": "text",
"width": 600,
"height": 600
},
{
"url": "url",
"text": "text",
"width": 600,
"height": 600
}
]
},
{
"type": 4,
"object": {
"id": 337203,
"type": 1,
"city": "1"
}
}
]
This json structure is inherently gson-unfriendly. i.e You cannot model this cleanly in java because the "object" key refers to a dynamic type. The best you can do with this structure is model it like so:
public class Models extends ArrayList<Models.Container> {
public class Container {
public int type;
public Object object;
}
public class Type1Object {
public String title1;
public String title2;
}
public class Type3Object {
public String url;
public String text;
public int width;
public int height;
}
public class Type4Object {
public int id;
public int type;
public int city;
}
}
Then do some awkward switch on type and the object field:
String json = "{ ... json string ... }";
Gson gson = new Gson();
Models model = gson.fromJson(json, Models.class);
for (Models.Container container : model) {
String innerJson = gson.toJson(container.object);
switch(container.type){
case 1:
Models.Type1Object type1Object = gson.fromJson(innerJson, Models.Type1Object.class);
// do something with type 1 object...
break;
case 2:
String[] type2Object = gson.fromJson(innerJson, String[].class);
// do something with type 2 object...
break;
case 3:
Models.Type3Object[] type3Object = gson.fromJson(innerJson, Models.Type3Object[].class);
// do something with type 3 object...
break;
case 4:
Models.Type4Object type4Object = gson.fromJson(innerJson, Models.Type4Object.class);
// do something with type 4 object...
break;
}
}
Ultimately the best solution is to get the json structure changed to something more compatible with java.
E.g:
[
{
"type": 1,
"type1Object": {
"title1": "title1",
"title2": "title2"
}
},
{
"type": 2,
"type2Object": [
"string",
"string",
"string"
]
},
{
"type": 3,
"type3Object": [
{
"url": "url",
"text": "text",
"width": 600,
"height": 600
},
{
"url": "url",
"text": "text",
"width": 600,
"height": 600
}
]
},
{
"type": 4,
"type4Object": {
"id": 337203,
"type": 1,
"city": "1"
}
}
]
This may be a bit late for the original poster, but hopefully it will help someone else.
I am using Gson
in Android
.
I have seen everyone use custom classes and long way round solutions.
Mine is basic.
I have an ArrayList of many different Object types (Models for my database) - Profile is one of them. I get the item using mContactList.get(i)
which returns:
{"profile":
{"name":"Josh",
"position":"Programmer",
"profile_id":1,
"profile_image_id":10,
"user_id":1472934469
},
"user":
{"email":"[email protected]",
"phone_numbers":[],
"user_id":1,
"user_type_id":1
},
"follower":
{"follower_id":3,
"following_date":1.4729345E9,
"referred_by_id":2,
"user_from_id":1,
"user_to_id":2
},
"media":
{"link":"uploads/profiles/profile-photos/originals/1-G9FSkRCzikP4QFY.png",
"media_description":"",
"media_id":10,
"media_name":"",
"media_slug":"",
"medium_link":"uploads/profiles/profile-photos/thumbs-medium/1-G9FSkRCzikP4QFY.png",
"thumbnail_link":"uploads/profiles/profile-photos/thumbs-small/1-G9FSkRCzikP4QFY.png",
"uploader_id":1
}
}
Now I create the Gson
object:
Gson gson = new Gson();
// this creates the JSON string you see above with all of the objects
String str_obj = new Gson().toJson(mContactList.get(i));
Now instead of creating a custom class - just pass it through as a JsonObject
using the following code:
JsonObject obj = gson.fromJson(str_obj, JsonObject.class);
And now, you can call the object like so:
JsonObject profile = obj.getAsJsonObject("profile");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With