Convert json String to array of Objects

18,342

Try this

Address[] address = gson.fromJson(addressJson, Address[].class);
Share:
18,342
jackyesind
Author by

jackyesind

Updated on June 18, 2022

Comments

  • jackyesind
    jackyesind almost 2 years

    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)
    
  • Brian
    Brian over 10 years
    +1. Also, if a collection is preferred like List, you can wrap the fromJson call in Arrays.asList
  • jackyesind
    jackyesind over 10 years
    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)
  • Narendra Pathai
    Narendra Pathai over 10 years
    See this link for that stackoverflow.com/questions/6873020/gson-date-format You will have to change the default date format for GSON
  • Alexander Gapanowich
    Alexander Gapanowich about 5 years
    Thank you! It's very useful!