7

I have json array as string

[
        {
        "id":"1",   
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
        "id":"2",   
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "id":"3",   
        "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "id":"4",   
        "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "id":"5",   
        "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        }
]

I want to convert this string to each json object. I have a class address. How would i convert this json string to object

I tried

Address address = gson.fromJson(addressJson, Address.class);

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
    com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    com.google.gson.Gson.fromJson(Gson.java:803)
    com.google.gson.Gson.fromJson(Gson.java:768)
    com.google.gson.Gson.fromJson(Gson.java:717)
    com.google.gson.Gson.fromJson(Gson.java:689)
jackyesind
  • 3,343
  • 14
  • 47
  • 74

2 Answers2

23

Try this

Address[] address = gson.fromJson(addressJson, Address[].class);
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • 4
    +1. Also, if a collection is preferred like `List`, you can wrap the `fromJson` call in [`Arrays.asList`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)) – Brian Aug 27 '13 at 06:49
  • It is working but for the same thing i have one field for date with value `Tue Aug 27 06:07:32 UTC 2013`. It says error on `java.text.ParseException: Unparseable date: "Tue Aug 27 11:37:32 IST 2013" java.text.DateFormat.parse(DateFormat.java:337)` – jackyesind Aug 27 '13 at 06:54
  • 1
    See this link for that http://stackoverflow.com/questions/6873020/gson-date-format You will have to change the default date format for GSON – Narendra Pathai Aug 27 '13 at 06:56
  • Thank you! It's very useful! – Alexander Gapanowich Mar 28 '19 at 23:34
0

Using Jackson's ObjectMapper class,you can read values and map them to an object, or an array of objects.

final ObjectMapper objectMapper = new ObjectMapper();
Address [] mdls = objectMapper.readValue(addressJson, Address [].class);

For List<Address> use:

List<Address> mdls = objectMapper.readValue(addressJson, new TypeReference<List<Address>>() {});
TiyebM
  • 2,684
  • 3
  • 40
  • 66