2011-11-25 120 views
0

我得到了我的一个Android服务服务器响应的以下json响应,即时通讯有问题传递这个响应,有人可以帮助我做到这一点,即时通讯使用gson传递json响应。Gson反序列化抛出JsonParseException:期望找到的对象:

感谢

山姆

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

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

让我们来看看,您有:

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