android json 解析

笔者是刚入门的菜鸟,不足之处,请多多指教,谢谢。

笔者的网络请求用的是OKHTTP,JSON解析用的是GSON。

下面是请求成功的返回方法:

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            // 请求的结果
            String result = response.body().string();
            // 新建gson
            Gson gson = new Gson();
            JSONObject jsonObject = null;
            try {
                // json 格式化
                jsonObject = new JSONObject(result);
                // 这里的返回的是数组格式的json
                JSONArray jsonArray = jsonObject.getJSONArray("Data");
                // 通过循环 取出一个个的 object
                for(int i = 0; i < jsonArray.length(); i++) {
                    Product product = new Product();
                    JSONObject object = jsonArray.getJSONObject(i);
                    product.productid = object.getString("productid");
                    product.productname = object.getString("productname");
                    product.classid = object.getString("classid");
                    product.productspec = object.getString("productspec");
                    product.productpricethis = (float) object.getDouble("productpricethis");
                    product.productpic = object.getString("productpic");
                    product.productnumber = object.getString("productnumber");
                    data.add(product);
                }
                handler.sendEmptyMessage(1);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

上面的方法在object属性少的时候还能接受,但属性多了的话,就比较麻烦了,而且属性的名字也容易搞错,效率不高。

后来上网查了下,原来可以直接传一个bean类过去,直接就能完成属性的赋值,代码如下:

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            // 请求的结果
            String result = response.body().string();
            // 新建gson
            Gson gson = new Gson();
            JSONObject jsonObject = null;
            try {
                // json 格式化
                jsonObject = new JSONObject(result);
                // 这里的返回的是数组格式的json
                JSONArray jsonArray = jsonObject.getJSONArray("Data");
                // 通过循环 取出一个个的 object
                for(int i = 0; i < jsonArray.length(); i++) {
                    String objectStr = jsonArray.getString(i);
                    // 解析produc类
                    Product product = gson.fromJson(objectStr, Product.class);
                    // 添加到数组中
                    data.add(product);
                }
                handler.sendEmptyMessage(1);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

Product 类的每个属性都要实现各自的get、set方法,属性的类型要和返回的类型一致。

String objectStr = jsonArray.getString(i);
// 解析produc类
Product product = gson.fromJson(objectStr, Product.class);

主要是把属性赋值的代码换成上面的两句代码来实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值