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)
2

2 Answers 2

23

Try this

Address[] address = gson.fromJson(addressJson, Address[].class);
Sign up to request clarification or add additional context in comments.

3 Comments

+1. Also, if a collection is preferred like List, you can wrap the fromJson call in Arrays.asList
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)
See this link for that stackoverflow.com/questions/6873020/gson-date-format You will have to change the default date format for GSON
1

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>>() {});

Comments

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.