2014-02-17 79 views
8

我通過點擊url獲得一些json響應。我想用jackson來解析json響應。我嘗試使用對象映射器,但我收到異常。如何在android中使用Jackson分析json響應?

JSON:

{ 
    "contacts": [ 
     { 
       "id": "c200", 
       "name": "ravi raja", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     }, 
     { 
       "id": "c201", 
       "name": "Johnny Depp", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     }, 

    ] 
} 

POJO:

public class ContactPojo { 

    String name,email,gender,mobileno; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getGender() { 
     return gender; 
    } 

    public void setGender(String gender) { 
     this.gender = gender; 
    } 

    public String getMobileno() { 
     return mobileno; 
    } 

    public void setMobileno(String mobileno) { 
     this.mobileno = mobileno; 
    } 

} 

代碼:

ObjectMapper mapper=new ObjectMapper(); 
      userData=mapper.readValue(jsonResponse,ContactPojo.class); 
+0

發佈您的解析代碼。 – Hariharan

回答

5

正如我可以看到你JSON不是數組,但是對象,其保持包含陣列的一個對象所以你需要創建一個臨時的數據庫類來讓傑克遜解析它。

private static class ContactJsonDataHolder { 
    @JsonProperty("contacts") 
    public List<ContactPojo> mContactList; 
} 

public List<ContactPojo> getContactsFromJson(String json) throws JSONException, IOException { 

    ContactJsonDataHolder dataHolder = new ObjectMapper() 
     .readValue(json, ContactJsonDataHolder.class); 

    // ContactPojo contact = dataHolder.mContactList.get(0); 
    // String name = contact.getName(); 
    // String phoneNro = contact.getPhone().getMobileNro(); 
    return dataHolder.mContactList; 
} 

而小的調整你的類:

@JsonIgnoreProperties(ignoreUnknown=true) 
public class ContactPojo { 

    String name, email, gender; 
    Phone phone; 

    @JsonIgnoreProperties(ignoreUnknown=true) 
    public static class Phone { 

     String mobile; 

     public String getMobileNro() { 
       return mobile; 
     } 
    } 

    // ... 

    public Phone getPhone() { 
     return phone; 
    } 

@JsonIgnoreProperties(ignoreUnknown =真)註釋可以確保你沒有得到例外的時候你的類犯規包含屬性,它是在JSON ,就像你的json中的address可能會給出異常,或Phone對象中的home

+0

我有一個疑問,==> contact.getPhone String中phoneNro = contact.getPhone.getMobileNro(); – skyshine

+0

是的,謝謝,這麼快就寫錯了,所以我錯過了getPhone的'()':) – vilpe89

-1

示例JSON數據

{ 
    "records": [ 

    {"field1": "outer", "field2": "thought"}, 
    {"field2": "thought", "field1": "outer"} 
    ] , 
    "special message": "hello, world!" 
} 

你需要存儲在sample.json斷言forder然後代碼

try 
{ 

    InputStreamReader foodDataIn = new InputStreamReader(getAssets().open("sample.json")); 
    ObjectMapper mapper = new ObjectMapper(); 
    JsonParser jp = mapper.getFactory().createParser(foodDataIn); 
    JsonToken current; 

    current = jp.nextToken(); 
    if (current != JsonToken.START_OBJECT) { 
     System.out.println("Error: root should be object: quiting."); 
     return; 
    } 
    while (jp.nextToken() != JsonToken.END_OBJECT) { 
     String fieldName = jp.getCurrentName(); 
     // move from field name to field value 
     current = jp.nextToken(); 
     System.out.println("NAme: " +fieldName); 
     if (fieldName.equals("records")) { 
      if (current == JsonToken.START_ARRAY) { 
       // For each of the records in the array 
       while (jp.nextToken() != JsonToken.END_ARRAY) { 
        // read the record into a tree model, 
        // this moves the parsing position to the end of it 
        JsonNode node = jp.readValueAsTree(); 
        // And now we have random access to everything in the object 
        System.out.println("field1: " + node.get("field1").asText()); 
        System.out.println("field2: " + node.get("field2").asText()); 
       } 
      } else { 
       System.out.println("Error: records should be an array: skipping."); 
       jp.skipChildren(); 
      } 
     } else { 
      System.out.println("Unprocessed property: " + fieldName); 
      jp.skipChildren(); 
     } 
    }  
} catch (IOException e) { 
    e.printStackTrace(); 
} 
2

嗯,我總是用jsonschema2pojo.org創建我的模型/ POJO類!

你需要提供你的json數據,並根據這些數據爲你創建pojo/Model類!很酷 !

+0

不直接回答這個問題,但它是一個非常有用的工具,我以前沒有見過! – SacredSkull