11

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.

1
  • Just want to stress that the class file is CASE SENSITIVE! Just encountered this issue and took me half day to find this dumb mistake
    – JamesYTL
    Nov 15, 2017 at 3:46

5 Answers 5

5

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.

1
  • Your answer gave me an idea and is the closest to the solution I have so I'm gonna accept yours. Will post my solution in the question.
    – JamesYTL
    Jul 1, 2016 at 2:52
1

Look at the API's of JSONObject and JSONArray.

You should be able to figure it out from there. You just create a JSONObject out of the string:

ex. JSONObject jsonAsObj = new JSONObject(my_json_string);

Then use JSONArray classArray = jsonAsObject.getJSONArray("Class"); to get your array...

As far as converting it to an ArrayList, you need to create your type and traverse the JSONArray and create objects from it, and add them to your ArrayList. Or you could look into another helper library, like GSON.

1

I believe this is an invalid Json. Your class attribute is an array and an object.

3
  • ^^ upvoted. Putting the provided String in a viewer shows that it's invalid json, so creating your JSONObject should throw an exception.
    – Submersed
    Jul 1, 2016 at 2:24
  • 1
    Should be a comment, not an answer.
    – shmosel
    Jul 1, 2016 at 2:27
  • Updated. Inserted an older version of Json string. Bad mistake for typing for the json string my own :p
    – JamesYTL
    Jul 1, 2016 at 2:31
0

You need to use GSON library for example:

@SerializedName("studentName") public String name;

0

There is a need to add a somewhat missing feature in the above first(thealeksandr)'s answer:

 Gson gson = new GsonBuilder().create();

You can now use this instantiated object gson in the above ClassObj object = gson.fromJson(jsonString, ClassObj.class); To do this simply use GsonBuilder class in the Gson Library as I just demonstrated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.