2017-08-28 3 views
0

Je me bats pour analyser le dessous JSON en utilisant les bibliothèques jacksonComment analyser la réponse json ci-dessous en utilisant jackson 2.X? Le support [après le résultat est à l'origine question

{ "résultat": [{ "UserID": "xyz", "prenom": "abc", » lastName ":" def "," vFlag ":" faux "," URL ":" xyz: //abc.com/cti.do?sysparm_caller = abc% 20def & sysparm_caller_phone = + 1 800 123 456 "}]}

le crochet après le "résultat" semble être à l'origine du problème. J'ai déjà UNWRAP_ROOT_VALUE dans mon code. mapper.configure (DeserializationFeature.UNWRAP_ROOT_VALUE, true);

Si je retire le [] alors le json to pojo fonctionne bien. Est-ce que je peux utiliser n'importe quelle annotation sans bidouiller avec des manipulations de cordes?

POJO classe

package com.parse.input; 
import com.fasterxml.jackson.annotation.JsonRootName; 

@JsonRootName(value = "result") 
public class Employee { 
    private String userID = null; 
    private String firstName = null; 
    private String lastName = null; 
    private String vFlag = null; 
    private String uRl = null; 
    public String getUserID() { 
     return userID; 
    } 
    public void setUserID(String userID) { 
     this.userID = userID; 
    } 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
    public String getVFlag() { 
     return vFlag; 
    } 
    public void setVFlag(String vipFlag) { 
     this.vFlag = vipFlag; 
    } 
    public String getURL() { 
     return uRl; 
    } 
    public void setURL(String ctiURL) { 
     this.uRl = ctiURL; 
    } 
} 

====== et le code pour appeler API REST et analyser la réponse ======

Client restClient = Client.create();    
WebResource webResource = restClient.resource(wURL);    
ClientResponse resp = webResource.accept(MediaType.APPLICATION_JSON)            
     .header("Authorization", "Basic " + authStringEnc) 
     .header("EXT_URL", sURL + inputParameter) 
     .get(ClientResponse.class); 

String output = resp.getEntity(String.class); 
ObjectMapper mapper = new ObjectMapper(); 
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); 
try { 
    System.out.println("Starting to parse the employee response"); 
    Employee employee = mapper.readValue(ouput.toString(), Employee.class); 
    } catch (JsonParseException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JsonMappingException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+0

Postez le code du fichier. –

Répondre

0

j'ai pu la contourner comme ça.

try{ 
    JSONObject j=new JSONObject(output.toString()); 
    extractedJsonEmployee = j.getJSONArray("result").get(0).toString(); 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
//Commented out the UNWRAP_ROOT_VALUE, since i am already taking out the root 
Employee employee = mapper.readValue(extractedJsonEmployee, Employee.class);