Sam
Sam

Reputation: 6395

Gson deserialize throws JsonParseException: Expecting object found:

I'm getting the following json response for one of my android service server response,im having problem to pass this response,can some one help me to do this,im using gson to pass the json response.

Thanks

Sam

Exception im getting:
11-25 22:18:25.250: W/System.err(1590): com.google.gson.JsonParseException: Expecting object found: 2

CODE

 // Execute HTTP Post Request
            HttpResponse httpResponse = httpclient.execute(httpPost);
            HttpEntity resEntity = httpResponse.getEntity();
            if (resEntity != null) {
                String resp = EntityUtils.toString(resEntity);
                Log.i("RESPONSE", resp);
                GsonBuilder gsonb = new GsonBuilder();
                Gson gson = gsonb.create();
                UserSearchServerResponse resonse = null;
                JsonParser parser = new JsonParser();
                if (parser.parse(resp).isJsonObject()) {
                    resonse = gson.fromJson(resp, UserSearchServerResponse.class);
                } else if (parser.parse(resp).isJsonArray()) {
                    Type listType = new TypeToken<ArrayList<Map<String,Map<String,User>>>>() {
                    }.getType();
                    ArrayList<Object> searchResultList = new Gson().fromJson(resp, listType);
                    Log.i("Search RESPONSE", "searchResultList.size()" + searchResultList.size());
                    resonse = new UserSearchServerResponse();
                    resonse.setSearchResult(searchResultList);
                }

                return resonse;
            }


Server response:

[
    {
        "4ecf08a783ba88c400000004": {
            "type": 2,
            "name": "ddd",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4e815bd583ba88f53e000000": {
            "type": 1,
            "name": "xxxx",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec3293f83ba88c600000000": {
            "type": 2,
            "name": "ddsdssd",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec3dd4c83ba88c200000000": {
            "type": 2,
            "name": "ddd",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec4074683ba88d500000001": {
            "type": 2,
            "name": "123456",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec40a9e83ba88bf00000000": {
            "type": 2,
            "name": "qwerty",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec4192a83ba88c000000002": {
            "type": 2,
            "name": "sds",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec419b983ba88c200000001": {
            "type": 2,
            "name": "sujeevqqwwii",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec4a17483ba88d500000002": {
            "type": 2,
            "name": "sujeev VP",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec60e5683ba88c500000000": {
            "type": 2,
            "name": "'dddd'",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    },
    {
        "4ec60f1e83ba88c900000001": {
            "type": 2,
            "name": "'dddd'",
            "photo": "default_profile.png",
            "coordinates": [
                0,
                0
            ]
        }
    }
]

Upvotes: 0

Views: 5730

Answers (2)

Programmer Bruce
Programmer Bruce

Reputation: 66943

The JSON structure in Java terms is a List<Map<String, User>>, where each map has just one entry.

import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Type listType = new TypeToken<ArrayList<Map<String, User>>>() {}.getType();
    ArrayList<Map<String, User>> searchResultList = new Gson().fromJson(new FileReader("input.json"), listType);
    System.out.println(searchResultList);
  }
}

class User
{
  int type;
  String name;
  String photo;
  int[] coordinates;

  @Override
  public String toString()
  {
    return String.format("User: type=%d, name=%s, photo=%s, coordinates=%s", type, name, photo, Arrays.toString(coordinates));
  }
}

Upvotes: 2

dmon
dmon

Reputation: 30168

Let's see, you have:

[  -- Arraylist
{   --- First object (Map)
    "4ecf08a783ba88c400000004": --- first Map key
    { -- Start of User value (presumably)
        "type": 2,
        "name": "ddd",
        "photo": "default_profile.png",
        "coordinates": [
            0,
            0
        ]
    } -- end of user
}, -- end of map

Which you declared as being a ArrayList<Map<String,Map<String,User>>>, I believe you have an extra Map there, it should be List<Map<String,User>>. However, your structure is a bit wonky, that Array seems unnecessary to me. I believe you really only want a Map<String,User>.

Upvotes: 1

Related Questions