Veeru
Veeru

Reputation: 4936

Android: Handling json objects that can be a string or array

Ran into a situation where am not sure how to handle it.

I have json data that comes from a server; for example:(am just posting part of the json, so, yes, the json is valid)

    "wall_id": 889149,
    "poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
    "post_type": "profile",
    "post_content": [{
        "text": "",
        "images_count": 1,
        "images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
    }]

Created a class to store this json data

public class feedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    String post_content;
}

There are times when post_content can be empty or an array as the example above. I have declared post_content as String in feedFormat. This is obviously throwing a cast exception (Converting array to string?).

I was expecting JSONObject to read it as a string and later convert it into an array from there, but does'nt seem to go that way.

How can i dynamically handle a string or an array? if it is an array, i need to break it down.

I am porting this app from IOS to android, there is a "id" object in IOS that can be of any class. I check if the class is a NSSTring or NSArray and take it from there. Here in Java, am not sure how to handle it.

Any suggestions are highly appreciated

Upvotes: 0

Views: 457

Answers (5)

Permita
Permita

Reputation: 5493

You can use something like this:

String data= "wall_id": 889149,
"poster_image_thumbnail": "http:\/\/www.mface.me\/images\/avatar\/thumb_62441559ddb1dda7513d0f94.jpg",
"post_type": "profile",
"post_content": [{
    "text": "",
    "images_count": 1,
    "images": ["https:\/\/fbcdn-sphotos-a-a.akamaihd.net\/hphotos-ak-ash4\/227408_475848819113499_663318592_n.jpg"]
}]

JSONArray jArray=data.getJSONArray("post_content");
for(int i=0; i<jArray.length(); i++)
{
JSONObject jObj=jArray.getJSONObject(i);
String text=jObj.getString("text");
int images_count=jObj.getInt("images_count");
String images=jObj.getInt("images");
}

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

you can check like this jsonobject.has("post_content")

if(jsonobject.has("post_content")) {
   /// read array and do remaining stuff
}else {
  // if not read another strings and put post_content as null.
}

Upvotes: 0

Cyril Maitre
Cyril Maitre

Reputation: 401

public class FeedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    JSONArray post_content;
}

feedFormat toto = new feedFormat();
toto.post_content = yourJsonObject.getJsonArray("post_content");

This is the easiest way to do what you want. Another way is to create another class.

public class FeedFormat{
    Integer wall_id;
    String poster_image_thumbnail;
    String post_type;
    ArrayList<PostContent> post_content = new ArrayList<PostContent>();
}

public class PostContent {
    String text;
    Integer imageCount;
    ArrayList<String> images = new ArrayList<String>();
}

With that you can handle each post content into specific object instead of use JSONObject / JSONArray.

Upvotes: 0

Vinay S Shenoy
Vinay S Shenoy

Reputation: 4028

JSONObject has functions called has(String key) which checks if there is a mapping for a key and isNull(String key) which checks if a particular key is null. Use these to check the key before reading.

Upvotes: 0

Rapha&#235;l Titol
Rapha&#235;l Titol

Reputation: 722

If your JSON array is empty, it will be like that :

"post_content": []

It will then remain an array, with the particularity of being 0-sized.

Then I suggest you parse directly your JSON array into a appropriate data structure, whatever the size, like an ArrayList> for example. You will then be able to go through all the items of your JSON array, and for each item, add a new HashMap in your arraylist. Every hashmap will contain there pairs of key values.

However, if I understand well your JSON, it seems that it will be always an array of three elements, the third element being itself a array, which size is given bu the attribute images_count. This is not very good, your JSON structure should be :

"post_content": {
    "text": "",
    "images": [
        "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-ash4/227408_475848819113499_663318592_n.jpg"
    ]
}

Since images is an array, you can easily get its size.

Upvotes: 1

Related Questions