如何解析Android中使用杰克逊JSON响应? [英] How to parse json response using Jackson in android?

查看:204
本文介绍了如何解析Android中使用杰克逊JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过敲击网址一些JSON响应。我想用杰克逊来解析JSON响应。我试着用对象映射器,但我得到的例外。

json的:

  {
    人脉:
        {
                ID:C200,
                名:拉维拉贾
                电子邮件:raja@gmail.com
                地址:XX-XX-XXXX,X  - 街道,X  - 国家,
                性别男,
                电话: {
                    手机:+91 0000000000,
                    家:00 000000,
                    写字楼:00 000000
                }
        },
        {
                ID:C201,
                名:约翰尼·德普
                电子邮件:johnny_depp@gmail.com
                地址:XX-XX-XXXX,X  - 街道,X  - 国家,
                性别男,
                电话: {
                    手机:+91 0000000000,
                    家:00 000000,
                    写字楼:00 000000
                }
        },

    ]
}
 

POJO:

 公共类ContactPojo {

    字符串名称,电子邮件,性别,mobileno;

    公共字符串的getName(){
        返回名称;
    }

    公共无效setname可以(字符串名称){
        this.name =名称;
    }

    公共字符串getEmail(){
        返回的电子邮件;
    }

    公共无效setEmail(字符串电子邮件){
        this.email =电子邮件;
    }

    公共字符串getGender(){
        返回性别;
    }

    公共无效setGender(字符串性别){
        this.gender =性别;
    }

    公共字符串getMobileno(){
        返回mobileno;
    }

    公共无效setMobileno(字符串mobileno){
        this.mobileno = mobileno;
    }

}
 

code:

  ObjectMapper映射器=新ObjectMapper();
             用户数据= mapper.readValue(jsonResponse,ContactPojo.class);
 

解决方案

我可以看到你JSON是不是数组,但对象保持包含一个数组,所以你需要在这里做出杰克逊创建一个临时dataholder类的一个对象解析它。

 私有静态类ContactJsonDataHolder {
    @JsonProperty(接触)
    公开名单< ContactPojo> mContactList;
}

公开名单< ContactPojo> getContactsFromJson(JSON字符串)抛出JSONException,IOException异常{

    ContactJsonDataHolder dataHolder =新ObjectMapper()
        .readValue(JSON,ContactJsonDataHolder.class);

    // ContactPojo接触= dataHolder.mContactList.get(0);
    //字符串名称= contact.getName();
    //字符串phoneNro = contact.getPhone()getMobileNro()。
    返回dataHolder.mContactList;
}
 

和小的调整你的类:

  @JsonIgnoreProperties(ignoreUnknown =真)
公共类ContactPojo {

    字符串名称,电子邮件,性别;
    手机手机;

    @JsonIgnoreProperties(ignoreUnknown =真)
    公共静态类电话{

         字符串移动;

         公共字符串getMobileNro(){
              返回移动;
         }
    }

    // ...

    公用电话getPhone(){
        回电话;
    }
 

@JsonIgnoreProperties(ignoreUnknown = TRUE)注释可以确保你没有得到的异常,当你的类犯规包含属性,它是在JSON,如地址在你的JSON可以给一个异常,或家居在电话对象。

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

}

code:

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

解决方案

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.

这篇关于如何解析Android中使用杰克逊JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆