9

I know there are many JSON with GSON questions but none of them relate to me directly. My JSON is formatted differently.

I have a JSON data I want to parse using GSON which looks like the following:

[
   {
    "foo":"1",
    "bar":[ { "_id":"bar1"} ],
    "too":["mall", "park"]
   }
]

And I have the model Classes:

ItemArray Class

public class ItemArray
{
   List<Item> itemArray;

   //Get set here
}

Item Class

public class Item
{
   String foo;
   List<Bar> bar;
   List<String> too;

   //Get set here
}

Bar Class

public class Bar
{
   String id;

   //Get set here
}

Heres the question. Is the JSON in the correct format? If so, are the model classes in the correct format?

If not, please shove me in the right direction. Thank you in advance!

PS. I can modify the JSON data format if need be.

4 Answers 4

8

According to your json, you should simply have :

public class ItemArray extends List<Item> {
}

if you want to keep you java class and change your json it should be :

{
 itemArray: [
     {
      "foo":"1",
      "bar":[ { "_id":"bar1"} ],
      "too":["mall", "park"]
     }
  ]
}

Oh, and there is a mismatch with the id and _id for Bar :

public class Bar
{
   String _id;

   //Get set here
}

You could also use an annotation to change the field's name during Json de/serialization.

And last but not least, consider typing your values better. Don't see any data as strings if they are not, you will not a lot of processing in java code to convert things. For instance :

"foo" : 1,

and see foo as an int data member, not a String.

Sign up to request clarification or add additional context in comments.

5 Comments

so in other words, all i need is that one class ItemArray + the Item class of course WITHOUT changing my JSON format?
I think so yes. And changing Bar from id to _id.
Actually that would be shorter than asking on SOF ;)
It works, after i fixed a date conversion issue. Thanks again. What i posted above is psuedo so that i don't reveal real data :/ NDAs if you understand
In this case you can have a model class that will set the list of items(List<ItemTypes>) in the json array. Have another class "ItemTypes" that will set all the individual items of the json array.
7

Some times we get JsonArray [ {..} , {..} ] as a response (without 'itemArray' name like yours) In that case you can use following code

Type fooType = new TypeToken<ArrayList<Item>>() {}.getType();
List<Item> array = new Gson().fromJson(response, fooType);

find more about this Official doc - Gson Array-Examples

1 Comment

The other answer only tells you what to change in the API to make it work, that isn't always possible. This is the right answer.
2

If you have a JsonArray like [ {..} , {..} ] you can do this with Gson:

Item[] items = gson.fromJson(json, Item[].class);

1 Comment

how do i parse this further...ie access String in Item.class?
0

To check Json is valid use this tool http://jsonlint.com/

Class Bar(
   private String _id;
   //create getter/setters
{

public class Item
{
   String foo;
   List<Bar> bar;
   List<String> too;

   //Get set here
}
//this is also fine
public class ItemList
{
   List<Item> itemArray;

   //Get set here
}

you named of list of items "itemArray", but in your json you have not named the corresponding array of items "itemArray". So make it itemArray, The problem is not in your json, it is valid. Problem is in its representation for Gson, Gson map keys of json on the variables of object (i.e Java POJO) with same name. If the name of your list is Class is

List<Item> itemArray;

then the corresponding json array name should also be itemArray, take a look blow

{
 itemArray: [
     {
      "foo":"1",
      "bar":[ { "_id":"bar1"} ],
      "too":["mall", "park"]
     }
  ]
}

so you can convert json into object like that

 Reader reader = new InputStreamReader(IOUtils.toInputStream(json_string));
 ItemList itemList = json.toObject(reader, ItemList.class);

Take a look into blow reference for more details https://stackoverflow.com/questions/13625206/how-to-parse-the-result-in-java/13625567#13625567

2 Comments

the first answer answered my question. but thank you for your answer and insight. I will keep that in mind in the future :D
What if i cannot change the json format. That is, how to set the model if i am not able to add the "itemArray" in the json? Thanks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.