2014-09-26 9 views
8

avere JSON dal servizio web, JSON Array come rispostaNo serializzatore trovato per la classe org.json.JSONObject e immobili scoperto per creare BeanSerializer

[3] 
    0: { 
    id: 2 
    name: "a561137" 
    password: "test" 
    firstName: "abhishek" 
    lastName: "ringsia" 
    organization: "bbb" 
     }- 
    1: { 
     id: 3 
    name: "a561023" 
password: "hello" 
    firstName: "hello" 
    lastName: "hello" 
    organization: "hello" 
    }- 
2: { 
    id: 4 
    name: "a541234" 
    password: "hello" 
firstName: "hello" 
    lastName: "hello" 
    organization: "hello" 
    } 

dopo avere ottenuto risposta in JsonArray errore durante la lettura Ottenere JSON oggetto di jSON Array:

 List<User> list = new ArrayList<User>(); 
     JSONArray jsonArr = new JSONArray(response); 
      for (int i = 0; i < jsonArr.length(); i++) { 
     JSONObject jsonObj = jsonArr.getJSONObject(i); 
     ObjectMapper mapper = new ObjectMapper(); 
     User usr= mapper.convertValue(jsonObj, User.class); 
     list.add(usr); 

    } 

No serializzatore trovato per la classe org.json.JSONObject e immobili scoperto per creare BeanSerializer (per evitare eccezioni, disabilitare SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))

risposta

2

deve accettare prima come array JSON, quindi durante la lettura dell'oggetto deve utilizzare Object Mapper.readValue, perché Json Object Still in String.

List<User> list = new ArrayList<User>(); 
JSONArray jsonArr = new JSONArray(response); 

for (int i = 0; i < jsonArr.length(); i++) { 
    JSONObject jsonObj = jsonArr.getJSONObject(i); 
    ObjectMapper mapper = new ObjectMapper(); 
    User usr = mapper.readValue(jsonObj.toString(), User.class);  
    list.add(usr); 
} 
Problemi correlati