Gson反序列化抛出JsonParseException:期望找到的对象:

时间:2011-11-25 16:58:48

标签: android gson json

我得到了以下json响应我的一个android服务器响应,我有问题传递这个响应,有人可以帮我做这个,我用gson传递json响应。

由于

萨姆

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
            ]
        }
    }
]

2 个答案:

答案 0 :(得分:2)

Java术语中的JSON结构是List<Map<String, User>>,其中每个地图只有一个条目。

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));
  }
}

答案 1 :(得分:1)

让我们看看,你有:

[  -- 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

你宣称自己是ArrayList<Map<String,Map<String,User>>>,我相信你有一个额外的地图,它应该是List<Map<String,User>>。但是,你的结构有点不稳定,我认为Array不是必需的。我相信你真的只想要Map<String,User>

相关问题