码迷,mamicode.com
首页 > 移动开发 > 详细

Android的在线解析Json

时间:2015-07-27 14:33:29      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

 前提是引用Volley的异步机制,引入它的jar包

 

public void getJSONByVolley() {
    //mContext为上下文,
        RequestQueue requestQueue = Volley.newRequestQueue(mContext);
        String JSONDataUrl = "http://sjshop.easyder.com/app/order_index/getCart?buyer_id=511";
        final ProgressDialog progressDialog = ProgressDialog.show(mContext, "正在下载", "Loading......");
        progressDialog.setCancelable(true);//progressDialog可以取消
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.GET,
                JSONDataUrl,
                null,
                new Response.Listener<JSONObject>() {


                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            getcartJson(response);
                        } catch (Exception e) {
                            e.getMessage();
                        }
                        if (progressDialog.isShowing() && progressDialog != null) {
                            progressDialog.dismiss();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError arg0) {
                        System.out.println("sorry,Error");
                    }
                });
        requestQueue.add(jsonObjectRequest);
        Log.i(TAG, "shopBeanList ---->解析完Json   ");

    }
    //getcart的网络解析,参数-->JSONObject
    public void getcartJson(JSONObject jsonObject) {
//        创建商店列表
        List<ShopBean> shopBeanList= new ArrayList<ShopBean>();
        JSONObject objectInfo = null;
        try {
            objectInfo = jsonObject.getJSONObject("info");
            JSONArray arraygroup = objectInfo.getJSONArray("group");
            for (int i = 0; i <arraygroup.length() ; i++) {
                JSONObject item = arraygroup.getJSONObject(i);
                ShopBean shopBean = new ShopBean(item);
                shopBeanList.add(shopBean);
                Log.d(TAG, "shopBeanList-----> "+ item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(mCallback!=null){
            mCallback.getDat(shopBeanList);
        }
    }

实体类中添加了一个JsonObject的构造函数

ShopBean实体类:

/**
 * Created by Administrator on 2015/7/19 0019.
 */
public class ShopBean {
    private int seller_uid;//店铺ID
    private String seller_name;//店铺名称
    public List<GoodsBean> goodsList;//商品信息列表

    public String getSeller_name() {
        return seller_name;
    }

    public void setSeller_name(String seller_name) {
        this.seller_name = seller_name;
    }

    public int getSeller_uid() {
        return seller_uid;
    }

    public List<GoodsBean> getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List<GoodsBean> goodsList) {
        this.goodsList = goodsList;
    }

    public void setSeller_uid(int seller_uid) {
        this.seller_uid = seller_uid;
    }

    public ShopBean() {

    }

    //构造函数,参数为JSONObject型
    public ShopBean(JSONObject jsonObject) {
        this.seller_uid = jsonObject.optInt("seller_uid");
        this.seller_name = jsonObject.optString("seller_name");
        GoodsBean goodsBean;
        JSONArray jsonArray = jsonObject.optJSONArray("goods");
        if (jsonArray != null && jsonArray.length() > 0) {
            try {
                goodsList = new ArrayList<>();
                for (int j = 0; j < jsonArray.length(); j++) {
                    goodsBean = new GoodsBean(jsonArray.getJSONObject(j));
                    goodsList.add(goodsBean);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

}

GoodsBean实体类:

技术分享
/**
 * Created by Administrator on 2015/7/24.
 */
public class GoodsBean {
    private int stock_id;//商品ID
    private String goods_name;//商品名称
    private int qty;//商品的星级
    private boolean is_choose;//是否选中
    private double price;//单价
    private String goods_img;//图片链接
    private String extend;//图片拓展
    private int stock_num;//商品数量
    private double goodsTotalPrice;//商品总价
    private double rate;//商品税率

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }

    public boolean is_choose() {
        return is_choose;
    }

    public void setIs_choose(boolean is_choose) {
        this.is_choose = is_choose;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getGoods_img() {
        return goods_img;
    }

    public void setGoods_img(String goods_img) {
        this.goods_img = goods_img;
    }

    public String getExtend() {
        return extend;
    }

    public void setExtend(String extend) {
        this.extend = extend;
    }

    public int getStock_num() {
        return stock_num;
    }

    public void setStock_num(int stock_num) {
        this.stock_num = stock_num;
    }

    public double getGoodsTotalPrice() {
        return goodsTotalPrice;
    }

    public void setGoodsTotalPrice(double goodsTotalPrice) {
        this.goodsTotalPrice = goodsTotalPrice;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public int getStock_id() {
        return stock_id;
    }

    public void setStock_id(int stock_id) {
        this.stock_id = stock_id;
    }

    public GoodsBean(JSONObject jsonObject){
        this.stock_id = jsonObject.optInt("stock_id");
        this.goods_name = jsonObject.optString("goods_name");
        this.qty = jsonObject.optInt("qty");
        this.is_choose = jsonObject.optBoolean("is_choose");
        this.price = jsonObject.optDouble("price");
        this.goods_img = jsonObject.optString("goods_img");
        this.extend = jsonObject.optString("extend");
        this.stock_num = jsonObject.optInt("stock_num");
        this.goodsTotalPrice = jsonObject.optDouble("goodsTotalPrice");
        this.rate = jsonObject.optDouble("rate");
    }

    public GoodsBean(){

    }
    @Override
    public String toString() {
        return  "等级:" + qty + " " +
                "税率:" + rate
                ;
    }
}
View Code

 

Android的在线解析Json

标签:

原文地址:http://www.cnblogs.com/shuyongzai/p/4679917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!