欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

json数据解析

程序员文章站 2022-05-30 21:49:54
...
*//**
* {"ret":0,"message":"","data":[{"success":true,"errorMessage":"","shopNumber":"NO1212121216",
* "dispatchNumber"
* :"RD259229301CN","sum":87,"weightSum":1.494,"volumneSum":0,"From":{"name":"ghosthorse","tel"
* :"18612924827"
* ,"country":"CN","region":"广东,广州市,白云区(人和镇、太和镇、钟落潭镇)","street":"航联商城西区2楼","unit":null,"postcode"
* :"510000"},"To":{"name":"Kholdeev Anton Aleksandrovich","tel":"7-9043309563","country":"RU","region":
* "Курганская область,Притобольный р-н"
* ,"street":"Москва/москва ул.Привольная д.19 кв.38","unit":null,"postcode"
* :"196567"},"transport":{"name":"Parcel","cost":79},"addService":[{"name":"XRU小包操作费","cost":8}]}]}
*//*

private CourierResponse<CourierOrderDetailInfo> backQueryCourierOrderRes(String jsonStr)
{
CourierResponse<CourierOrderDetailInfo>  cr=new CourierResponse<CourierOrderDetailInfo>();
try
{
     JSONObject json=new JSONObject(jsonStr);
     if(!json.isNull("ret")&&json.getInt("ret")==0){
         cr.addCommands(CourierRespStatus.SUCCESS);
        
         List<CourierOrderDetailInfo> data=new ArrayList<CourierOrderDetailInfo>();
         cr.setData(data);
         JSONArray jarray=json.getJSONArray("data");
         int size=jarray.length();
         for(int i=0;i<size;i++){
             JSONObject jo= jarray.getJSONObject(i);
             CourierOrderDetailInfo q=new CourierOrderDetailInfo();
             q.setShipNumber(jo.getString("shopNumber"));
             q.setTrackingNumber(jo.getString("dispatchNumber"));
             ShipmentCost cost=new ShipmentCost(jo.getInt("sum"),AppInfo.currency);
             q.setTotalCost(cost);
             Weight weight=new Weight(jo.getString("weightSum"),AppInfo.weightUnit);
             q.setWeight(weight);
             Volume volume=new Volume(jo.getString("volumneSum"),AppInfo.volumeUnit);
             q.setVolume(volume);
            
             JSONObject fu=jo.getJSONObject("From");
             Address address=new Address(fu.getString("country"), null,null,
                     new Place(fu.getString("region")), fu.getString("street"),
                     fu.getString("unit"),  fu.getString("postcode"));
             User fromUser=new User(fu.getString("name"),new Telephone(fu.getString("tel")),address);
             q.setSender(fromUser);
             JSONObject tu=jo.getJSONObject("To");
             Address address2=new Address(tu.getString("country"), null,null,
                     new Place(tu.getString("region")), tu.getString("street"),
                     tu.getString("unit"),  tu.getString("postcode"));
             User toUser=new User(tu.getString("name"),new Telephone(tu.getString("tel")),address2);
             q.setRecipient(toUser);
            
             JSONObject tp=jo.getJSONObject("transport");
             CourierProduct courierProduct=new CourierProduct(null,
                      tp.getString("name"),null,new ShipmentCost(tp.getDouble("cost"),AppInfo.currency));
             q.setCourierProduct(courierProduct);
            
             JSONArray si=jo.getJSONArray("addService");
             int m=si.length();
             List<CourierProduct> list=new ArrayList<CourierProduct>();
             for(int n =0;n<m;n++){
                 JSONObject sj= si.getJSONObject(n);
                 CourierProduct serviceItem=new CourierProduct(null,sj.getString("name"),null,new ShipmentCost(sj.getDouble("cost"),AppInfo.currency));
                 list.add(serviceItem);
             }
             q.setValueAddServices(list);
            
             data.add(q);
         }
        
     }else{
         cr.addCommands(CourierRespStatus.Error(json.getString("message")));
     }
}
catch (JSONException e)
{
     e.printStackTrace();
     cr.addCommands(CourierRespStatus.ERROR);
}
return cr;
}
相关标签: json