Abhishek Kumar Abhishek Kumar - 20 days ago 5
JSON Question

How to deserialize a json data using GSON?

[{"contacts":
[
{
"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>> {
@Override
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

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().

Comments