Android Jackson解析json

最近在一个开源项目中发现使用Jackson解析json,后来百度发现对jackson的好评是一面倒,决定在自己的项目中尝试下,为了减少网络的影响,所以使用应用内部数据,下面是Json的数据

{"status": {"respCode": "0000","respMsg": "获取信息成功"},"paging": {"pagecount": 10,"rowcount": 305,"totalpages": 31},"info": [{"uuid": "4028804d3e8beffc013e8c086aef000c","name": "卡梅伦","mobile": "13111122211","isdefault": "false","province": "北京","city": "北京市","zone": "朝阳区","address": "阳朝"},{"uuid": "5028804d3e8beffc013e8c086aef001c","name": "李昌钰","mobile": "15111122233","isdefault": "false","province": "北京","city": "北京市","zone": "石景山区","address": "首钢家属院"},{"uuid": "8028804d3e8beffc013e8c086aef021c","name": "考克斯","mobile": "18111122233","isdefault": "true","province": "天津","city": "天津","zone": "塘沽","address": "塘沽经济技术开发区"}]}

Status.java

public class Status {
    private String respCode, respMsg;

    public String getRespCode() {
        return respCode;
    }

    public void setRespCode(String s) {
        this.respCode = s;
    }

    public String getRespMsg() {
        return respMsg;
    }

    public void setRespMsg(String s) {
        this.respMsg = s;
    }

    @Override
    public String toString() {
        return "Status [_respCode=" + respCode + ", _respMsg=" + respMsg
                + "]";
    }
    
  }

Paging.java

public class Paging {
    private String pagecount;
    private String rowcount;
    private String totalpages;
    public String getPagecount() {
        return pagecount;
    }
    public void setPagecount(String s) {
        this.pagecount = s;
    }
    public String getRowcount() {
        return rowcount;
    }
    public void setRowcount(String s) {
        this.rowcount = s;
    }
    public String getTotalpages() {
        return totalpages;
    }
    public void setTotalpages(String s) {
        this.totalpages = s;
    }
    @Override
    public String toString() {
        return "Paging [_pagecount=" + pagecount + ", _rowcount=" + rowcount
                + ", _totalpages=" + totalpages + "]";
    }
    
}

DeliveryInfo.java

public class DeliveryInfo implements Parcelable{
    
    private String uuid;
    private String name;
    private String mobile;
    private String isdefault;
    private String province;
    private String city;
    private String zone;
    private String address;
    
    public String getUuid() {
        return uuid;
    }
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public String getIsdefault() {
        return isdefault;
    }
    public void setIsdefault(String isdefault) {
        this.isdefault = isdefault;
    }
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getZone() {
        return zone;
    }
    public void setZone(String zone) {
        this.zone = zone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "DeliveryInfo [uuid=" + uuid + ", name=" + name + ", mobile="
                + mobile + ", isdefault=" + isdefault + ", province="
                + province + ", city=" + city + ", zone=" + zone + ", address="
                + address + "]";
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeString(uuid);
        arg0.writeString(name);
        arg0.writeString(mobile);
        arg0.writeString(isdefault);
        arg0.writeString(province);
        arg0.writeString(city);
        arg0.writeString(zone);
        arg0.writeString(address);
        
    }
    
    public static final Parcelable.Creator<DeliveryInfo> CREATOR = new Creator<DeliveryInfo>() {  
        public DeliveryInfo createFromParcel(Parcel source) {  
            
            DeliveryInfo mDeliveryInfo = new DeliveryInfo(); 
            mDeliveryInfo.uuid = source.readString();
            mDeliveryInfo.name = source.readString();
            mDeliveryInfo.mobile = source.readString();
            mDeliveryInfo.isdefault = source.readString();
            mDeliveryInfo.province = source.readString();
            mDeliveryInfo.city = source.readString();
            mDeliveryInfo.zone = source.readString();
            mDeliveryInfo.address = source.readString();
            return mDeliveryInfo;  
        }  
        public DeliveryInfo[] newArray(int size) {  
            return new DeliveryInfo[size];  
        }  
    };
}

DeliveryAddInfo.java

public class DeliveryAddInfo {
    
    private Paging paging;
    public Paging getPaging() {return paging;}
    public void setPaging(Paging p) {this.paging = p;}
    
    private Status status;
    public Status getStatus() { return status; }
    public void setStatus(Status n) { status = n; }

    private List<DeliveryInfo> info;
    public List<DeliveryInfo> getInfo() {return info;}
    public void setInfo(List<DeliveryInfo> info) {this.info = info;}
    @Override
    public String toString() {
        return "DeliveryAddInfo [paging=" + paging.toString() + ", status=" + status.toString()
                + ", info=" + info.size() + "]";
    }
}

DeliveryAddJson_1.java

public class DeliveryAddJson_1 extends DeJson{
    private static final String TAG = "DeliveryActivity";
    private String description;
    private List<DeliveryInfo> deliveryList = new ArrayList<DeliveryInfo>();
    
    private JsonGenerator jsonGenerator = null;
    private ObjectMapper objectMapper = null;
    
    @Override
    public boolean decoderJson(String s) {
        deliveryList.clear();
        
        //判断返回值是否为空
        if("".equals(s)){
            description = Err.formatError(Err.HTTP_ERROR_DATA_ERROR);
            return false;
        }
        objectMapper = new ObjectMapper();
        try {
            jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(s);
        LogUtil.d(TAG, "-------start----------");
        try {
            DeliveryAddInfo info = objectMapper.readValue(s,new TypeReference<DeliveryAddInfo>() {});
            System.out.println(info.getInfo().size());
            deliveryList = info.getInfo();
            for(DeliveryInfo item : deliveryList){
                System.out.println(item.getName());
            }
            System.out.println(info.toString());
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        LogUtil.d(TAG, "-------end----------");
        return true;
    }
    @Override
    public String getDescription() {
        // TODO Auto-generated method stub
        return description;
    }
    public List<DeliveryInfo> getInfo(){
        return deliveryList;
    }

}

解析耗时如图所示:

 

调用系统自身的json解析 耗时如下:

目前来说,感觉jackson解析不如系统自身json解析,但是网上的评论是jackson解析是最快的,暂时费解,等以后想明白了再补充了......

 

posted @ 2013-05-27 15:47  似水流云  阅读(799)  评论(0编辑  收藏  举报