3

I am able to parse the following data into a java object:

{
    "name": "testname",
    "address": "1337 455 ftw",
    "type": "sometype",
    "notes": "cheers mate"
}

using this code:

public class Test 
{
    public static void main (String[] args) throws Exception
    {
        URL objectGet = new URL("http://10.0.0.4/file.json");

        URLConnection yc = objectGet.openConnection();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yc.getInputStream()));

        Gson gson = new Gson();

        try {
            DataO data = new Gson().fromJson(in, DataO.class);

            System.out.println(data.getName());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }      
}

But now I want to store a list of these objects out of the following JSON String:

[
    {
        "name": "testname",
        "address": "1337 455 ftw",
        "type": "sometype",
        "notes": "cheers mate"
    },
    {
        "name": "SumYumStuff",
        "address": "no need",
        "type": "clunkdroid",
        "notes": "Very inefficient but high specs so no problem."
    }
]

Could someone help me modify my code to do this?

2 Answers 2

5

You could specify the type to deserialize into as an array or as a collection.

As Array:

import java.io.FileReader;

import com.google.gson.Gson;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Data0[] data = new Gson().fromJson(new FileReader("input.json"), Data0[].class);
    System.out.println(new Gson().toJson(data));
  }
}

class Data0
{
  String name;
  String address;
  String type;
  String notes;
}

As List:

import java.io.FileReader;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    List<Data0> data = new Gson().fromJson(new FileReader("input.json"), new TypeToken<List<Data0>>(){}.getType());
    System.out.println(new Gson().toJson(data));
  }
}
1
  • Thanks Bruce, this is perfect. I ended up going with the List, but tried the Array out also and they both worked. Commented Jul 10, 2012 at 9:50
0

A quick look in the Gson User Guide indicates that this might not be possible since the deserializer doesn't know the type of the elements since you could have elements of different types in the array.

Collections Limitations

Can serialize collection of arbitrary objects but can not deserialize from it Because there is no way for the user to indicate the type of the resulting object While deserializing, Collection must be of a specific generic type

2
  • In the example question, I don't see any indication that the list could contain components of different types (or of different subtypes of a common parent type). Gson does have built-in handling for deserializing into a list or array of same-typed things. Commented Jul 8, 2012 at 23:55
  • Thanks for the reply. Any suggestions as to how would I work around this? Should I be using a different library to parse the Json? Should I be looking into this 'TypeToken' mentioned in the user guide you linked to? Commented Jul 8, 2012 at 23:56

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.