JamesYTL JamesYTL - 8 months ago 50
Android Question

Deserializing JSON string in android

I have JSON String that read as below

{ "Status":"Clear", "Class": [{

{"name":"personA", "available" : 1},

{"name":"personB", "available" : 0},

{"name":"personC", "available" : 0},

{"name":"personD", "available" : 1}

}] }

How do I deserialize the JSON String above?

I got the idea of using JSONObject to take in whole JSON String into it but no idea how to get the arraylist of object (the list of Class object) and assign to a arraylist<Class>.

Guidance and help are much appreciated.



I have solved this question but I think I have to explain how and why I used such solution.

So to further explain my question, this is an json string that originally an object that got serialized and sent from a webservice to my apps.

the original object is like this :

public class StatusJson
public String Status { get; set; }
public List<Class> Class { get; set; }

So what I have to do is just declare an exactly same class in my android then use this code

statusJson statusJSON=g.fromJson(JSonString,StatusJson.class);

which will automatically parse the json string to the exact same class format.

Hope this will help you guys too if you are directly sending a JSON serialized class like me.


I suggest you to check Gson library.

You can create a class with anotations

private class ClassObj {
private final String personA;

And then

ClassObj object = gson.fromJson(jsonString, ClassObj.class);

It can be complicated object, which contain other gson objects or Collection. Try.