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
]
}
}
]
The JSON structure in Java terms is a
List<Map<String, User>>, where each map has just one entry.