I have JSON String that read as below
{ "Status":"Clear", "Class": [{ <br>
{"name":"personA", "available" : 1}, <br>
{"name":"personB", "available" : 0}, <br>
{"name":"personC", "available" : 0}, <br>
{"name":"personD", "available" : 1} <br>
}] }
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.
UPDATED :
SOLVED AND SOLUTION SHOWN
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.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This chapter explains how to parse the JSON file and extract necessary information from it. Android provides four different classes to manipulate JSON data.
Json structure is made up with {}, [], comma, colon and double quotation marks and it includes the following data types: Object, Number, Boolean, String, and Array. Serialize means convert an object instance to an XML document. Deserialize means convert an XML document into an object instance.
Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch the required information by the program. JSON's main advantage is that it is a language-independent, and the JSON object will contain data like a key/value pair.
I suggest you to check Gson library.
You can create a class with anotations
private class ClassObj {
@SerializedName("personA")
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.
I believe this is an invalid Json. Your class attribute is an array and an object.
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