Convert json String to array of Objects
18,342
Try this
Address[] address = gson.fromJson(addressJson, Address[].class);
Author by
jackyesind
Updated on June 18, 2022Comments
-
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 over 10 years
-
jackyesind over 10 yearsIt 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 onjava.text.ParseException: Unparseable date: "Tue Aug 27 11:37:32 IST 2013" java.text.DateFormat.parse(DateFormat.java:337)
-
Narendra Pathai over 10 yearsSee this link for that stackoverflow.com/questions/6873020/gson-date-format You will have to change the default date format for GSON
-
Alexander Gapanowich about 5 yearsThank you! It's very useful!