(小知识点)android 获取json语句中数组的某一个值

通过http://blog.youkuaiyun.com/vincent_czz/article/details/7333977,找到了自己所需要的,--->写的更全面,  我写的是自己需要的部分             -----android菜鸟


json 语句:{
    "state": "OK",
    "type": [
        {
            "add_time": "2015-10-14",
            "attr_id": "86",
            "attr_name": "五金工具",
            "image": "http://www.layib.com/data/files/marchine_type/9155151.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "87",
            "attr_name": "市政园林",
            "image": "http://www.layib.com/data/files/marchine_type/7042480.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "88",
            "attr_name": "起重机械",
            "image": "http://www.layib.com/data/files/marchine_type/7828002.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "89",
            "attr_name": "工业车辆",
            "image": "http://www.layib.com/data/files/marchine_type/5010833.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "90",
            "attr_name": "路面设备",
            "image": "http://www.layib.com/data/files/marchine_type/7391571.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "91",
            "attr_name": "能源设备",
            "image": "http://www.layib.com/data/files/marchine_type/8926086.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "92",
            "attr_name": "路面设备",
            "image": "http://www.layib.com/",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "93",
            "attr_name": "物料运输",
            "image": "http://www.layib.com/data/files/marchine_type/2378234.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "94",
            "attr_name": "高空作业",
            "image": "http://www.layib.com/data/files/marchine_type/7522308.jpg",
            "parent_id": "79",
            "sort_order": "255"
        },
        {
            "add_time": "2015-10-26",
            "attr_id": "95",
            "attr_name": "建筑建材",
            "image": "http://www.layib.com/data/files/marchine_type/1149139.jpg",
            "parent_id": "79",
            "sort_order": "255"
        }
    ]
}


、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

使用的xutils获取的json语句


private List<String> getWuZiShaiXuanData(){
    List<String> wuziShaiXuanData = new ArrayList<>();

    HttpUtils httpUtils = new HttpUtils();

    httpUtils.send(HttpRequest.HttpMethod.GET, Utils.WUZI_ZULIN, new RequestCallBack<String>() {
        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            String result = responseInfo.result;

            try {
                JSONObject jsonObject = new JSONObject(result);

                JSONArray typeList = jsonObject.getJSONArray("type");

                int length = typeList.length();

                for (int i = 0;i<length;i++){
                    JSONObject oj = typeList.getJSONObject(i);
                    String name = (String) oj.get("attr_name");	

                    wuziShaiXuanData.add(name);
                }

                Toast.makeText(BaseActivity.this,wuziShaiXuanData.toString(),Toast.LENGTH_SHORT).show();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(HttpException e, String s) {

        }
    });

    return wuziShaiXuanData;
}


二.碰到的另一种json语句

{
    "state": "OK",
        "type": {
    "100": "xx",
            "101": "xx",
            "102": "xx",
            "103": "xx",
            "104": "xx",
            "105": "xx",
            "65": "xx",
            "66": "xx",
            "97": "xx",
            "98": "xx",
            "99": "xx"
	}
}



我自己的解析方式:

httpUtils.send(HttpRequest.HttpMethod.GET, Utils.BANZU_TYPE, new RequestCallBack<String>() {
    @Override
    public void onSuccess(ResponseInfo<String> responseInfo) {

        String result = responseInfo.result;

        try {
            JSONObject jsonObject = new JSONObject(result);

            String result_type = jsonObject.getString("type");

            jsonObject = new JSONObject(result_type);

            Iterator<String> iterator = jsonObject.keys();

            while (iterator.hasNext()) {

                String key = iterator.next();

                String value = jsonObject.getString(key);

                banzuData.add(value);      // List<String> banzuData


                banzuDataTypeHashMap.put(value,key);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onFailure(HttpException e, String s) {

    }
});





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值