2011-11-25 4 views
0

내 안드로이드 서비스 서버 응답에 다음 json 응답이 표시됩니다.이 응답을 전달하는 데 문제가 있습니다. gson을 사용하여이 작업을 수행 할 수 있습니까? json 응답을 전달하십시오.Gson deserialize throw 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>>. 그러나, 당신의 구조는 다소 불안정합니다, 그 배열은 불필요하게 보입니다. 나는 당신이 정말로 단지 Map<String,User>을 원한다고 생각한다.