Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse json response using Jackson in android?

I am getting some json response by hitting url. I want to use jackson to parse json response. I tried with object Mapper but I am getting exceptions.

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

}

code:

ObjectMapper mapper=new ObjectMapper();
             userData=mapper.readValue(jsonResponse,ContactPojo.class);
like image 314
skyshine Avatar asked Feb 17 '14 09:02

skyshine


People also ask

How does Jackson read JSON array?

Reading JSON from a File Thankfully, Jackson makes this task as easy as the last one, we just provide the File to the readValue() method: final ObjectMapper objectMapper = new ObjectMapper(); List<Language> langList = objectMapper. readValue( new File("langs. json"), new TypeReference<List<Language>>(){}); langList.

How do I read a JSON file using object mapper?

Read Object From JSON via URL ObjectMapper objectMapper = new ObjectMapper(); URL url = new URL("file:data/car. json"); Car car = objectMapper. readValue(url, Car. class);

What is Jackson Databind used for?

Data Binding API is used to convert JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations. It is of two type. Simple Data Binding - Converts JSON to and from Java Maps, Lists, Strings, Numbers, Booleans and null objects.


1 Answers

As I can see your json is not array but is object that holds one object containing an array so you need to create a temporary dataholder class where to make the Jackson parse it.

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

And little tweaks for your 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) annotation makes sure that you are not getting exceptions when your class doesnt contain property which is in the json, like address in your json could give an exception, OR home in Phone object.

like image 114
vilpe89 Avatar answered Sep 19 '22 11:09

vilpe89