如何在android中使用Jackson分析json响应?

我得到一些JSON响应通过点击url。 我想用jackson来parsingjson响应。 我试图用对象映射器,但我收到exception。

JSON:

{ "contacts": [ { "id": "c200", "name": "ravi raja", "email": "raja@gmail.com", "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": "johnny_depp@gmail.com", "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); 

Solutions Collecting From Web of "如何在android中使用Jackson分析json响应?"

正如我可以看到你的json不是数组,而是持有一个包含数组的对象的对象,所以你需要创build一个临时的数据库类,让jacksonparsing它。

 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; } 

对你的class级做些小小的调整

 @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 = true)注释可以确保当你的类不包含属于json的属性时,你不会收到exception,就像你的json中的address可以给出一个exception,或者在Phone对象中的home

那么我总是使用jsonschema2pojo.org创build我的Model / Pojo类!

你需要提供你的json数据,并根据这些数据为你创buildpojo / Model类! 很酷 !

示例Json数据

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

你需要在assert forder中存储一个sample.json,然后代码是

 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(); }