-1
    [{"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

Deserializer class is:

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.

Community
  • 1
  • 1
rozer
  • 131
  • 15

2 Answers2

1

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

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • plz help me with dis josn **data:[{key:value},null,{key:value}]** – Manish Nov 16 '16 at 11:58
  • @Manish If you have a question, you should ask a question (click the "Ask Question" link in upper right corner), not piggyback on an answer to the question of someone else. – Andreas Nov 16 '16 at 11:59
  • sry but i just want a suggestion that how i parsw that json – Manish Nov 16 '16 at 12:00
  • @Andreas I tried this: json.getAsJsonObject().get(CONTACTS).getAsJsonArray(); but still the error occurs. – rozer Nov 16 '16 at 12:01
  • 1
    @AbhishekKumar Of course, because the element in `json` is **not** a `JsonObject`, so `json.getAsJsonObject()` throws exception. Read my answer again, and try what I suggested, i.e. add `get(0)` between `getAsJsonArray()` and `getAsJsonObject()`. – Andreas Nov 16 '16 at 12:04
  • @Andreas Thank you very much. I did what you have suggested: json.getAsJsonArray().get(0).getAsJsonObject().get(CONTACTS).getAsJsonArray() & It now deserializes JSON data. – rozer Nov 16 '16 at 12:09
-1

It looks like you have your json inside [], which would indicate an array. Try:

{
    "contacts": 
     [
        {
            "name": "Ramesh Sippi",
            "email": "ramesh.sippi@gmail.com",
            "phone": 9972366543,
            "officePhone": 80012345676,
            "latitude": 18.5204,
            "longitude": 73.8567
        }
    ]
}
Chirag Parmar
  • 833
  • 11
  • 26
seanAshmore
  • 315
  • 2
  • 8