Abhishek Kumar Abhishek Kumar - 1 year ago 70
JSON Question

How to deserialize a json data using GSON?

"name": "Ramesh Sippi",
"email": "ramesh.sippi@gmail.com",
"phone": 9972366543,
"officePhone": 80012345676,
"latitude": 18.5204,
"longitude": 73.8567

I tried to deserialize above JSON but it shows :

java.lang.IllegalStateException: Not a JSON Object

public class Deserializer implements JsonDeserializer<List<ContactDetails>> {
public List<ContactDetails> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String CONTACTS = "contacts";
return new Gson().fromJson(json.getAsJsonArray().getAsJsonObject().getAsJsonArray(CONTACTS),typeOfT);

Please help me out.Any resource or tutorial which helped you in deserialization.

Answer Source

json.getAsJsonArray().getAsJsonObject() is the same as json.getAsJsonObject(), and that is the cause of your exception.

You likely meant json.getAsJsonArray().get(0).getAsJsonObject().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download