How to parse json response using Jackson in android? -


I'm getting some Jason response by hitting url and I want to use Jackson to parse Jackson's answer. I tried with an object mapper but I'm getting an exception.

json:

  {"contacts": [{"id": "c200", "name": "" male "," phone ":" "" "", "Email": "raja@gmail.com", "address": "xx-xx-xxxx, x - street, x - country", "gender" mobile ":" +91 0000000000 "," "Johnny Depp", "Email": "Johnny_depp@gmail.com", "": "000000", "Office": "00 000000"}, {"id": "c201", "name" Address: "xx-xx-xxxx, x - street, x - country", "gender": "male", "phone": {"mobile": "+91 0000000000", "home": "00 000000" , "Office": "00 000000"}},]}   

public class contact Pojo {string name, email, gender, Mobilino; public string getName () {return name;} public {Zero SetName (String Name) {this.name = name;} Public String getEmail () {Return email;} Public Zero Set Email (String Email) {this.email = email;} Public String Graphfinder {Return gender; } Public Zero Set Gender (String Lang) {the.gender = gender;} Public string Millboleano () {Return Mobilino;} Public Zero Set Mobilino (String Mobilino) {this.mobileno = mobileno; }}

Code:

  Object Mapper Mapper = New Object Mapper (); UserData = mapper.readValue (jsonResponse, ContactPojo.class); As I have seen that you do not have JS array, but an array in which an object contains an array so that you need to make a temporary dataholder square for parsing it where Jackson is  
  private Fixed category ContactJsonDataHolder {@JsonProperty ("Contacts") public list. & Lt; ContactPojo & gt; MContactList; } Public listing & lt; Contact page & gt; GetContactsFromJson (string json) throws JSONException, IOException {ContactJsonDataHolder dataHolder = new ObjectMapper () .readValue (json, ContactJsonDataHolder.class); // ContactsProject Contacts = DataHoldersMicContactItitGate (0); // string name = contact.getName (); // string phone nro = contact. Tags (). GetMobileNro (); Return dataholder.mccactquite; }   

And a little correction for your class:

  @JsonIgnoreProperties (ignoreUnknown = true) Public category ContactPojo {String name, email, gender, phone ; @JsonIgnoreProperties (unknown unknown = true) public stationary class phone {string mobile; Public string getMobileNro () {return mobile; }} // ... public phone getPhone () {returns phone; @JsonIgnoreProperties (ignoreUnknown = true) Annotation  Make sure that you are not getting the exception when your class doesnt have the property which is in json,  Address in your Jason / <> code exception, or  home  in the phone object.   

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -