android 如何解析复杂的json数据

来源:互联网 发布:log4j flume源码 编辑:程序博客网 时间:2024/05/17 22:00

大家好,项目中有处理很多比较复杂的json数据,例如:

{
"resultSuccess": true,
"data": {
"goodsInfo": {
"argument": "处理器@#&显卡@#&硬盘",
"path_name": "../images/goods/2017-08-01/20170801144513782246.jpg",
"bg_name": "经典台式",
"bg_id": "2"
},
"gaList": [{
"path_name": "../images/goods/2017-07-25/20170725101722818254.jpg"
},
{
"path_name": "../images/goods/2017-08-01/20170801144513782246.jpg"
}],
"argumentStrList": [{
"argument": "I5@#&750@#&500G",
"id": "12",
"price": "3500.00",
"promotionPrice": "3010.00"
}],
"arMap": {
"显卡": {
"1050": "3500.00",
"1080": "4000.00",
"750": "2950.00"
},
"处理器": {
"I3": "2950.00",
"I7": "4000.00",
"I5": "3500.00"
},
"硬盘": {
"500G": "2950.00",
"1T": "4000.00"
}
}
},
"resultCode": "CLS001",
"state": "01",
"resultDesc": "数据操作成功",
"bgId": "2"
}

我们单来看这个arMap,因为相对来说这个地方是比较复杂的,Map套Map,外层还是一个List,我是这样解析的:

try {
//将json转化为一个对象                        JSONObject jsonObject = new JSONObject(response);
//通过data字眼拿到data这个对象
                        JSONObject dataOb = (JSONObject) jsonObject.get("data");
//通过arMap字眼拿到arMap对象                        JSONObject arMapObj = (JSONObject) dataOb.get("arMap");                        arMap = arMapObj.toString();                    } catch (JSONException e) {                        e.printStackTrace();                    }
//Gson解析arMap,我这里用的treeMap,主要是因为,解析得到的数据是乱序的,这里是为了排序
                    TreeMap<String, TreeMap<String, String>> arMaps = new Gson().fromJson(arMap,                            new TypeToken<TreeMap<String, TreeMap<String, String>>>() {                            }.getType());//这样我们就得到了这个Map套Map的一个较为复杂的对象
//然后我们可以循环遍历拿到其中的key和value//                        List<String> arMapsKey = new ArrayList<String>();                    List<TreeMap<String, String>> valueList = new ArrayList<TreeMap<String, String>>();                    for (String pKey :                            arMaps.keySet()) {                        arMapsKey.add(pKey);                        if (arMapsKey.size() == 1) {                                                   } else if (arMapsKey.size() == 2) {                                                   } else if (arMapsKey.size() == 3) {                                                    }                    }                    for (TreeMap<String, String> pValue :                            arMaps.values()) {                        valueList.add(pValue);                    }                    if (valueList.size() == 2) {                                                for (String cCkey :                                valueList.get(0).keySet()) {                                                   }                        for (String cSkey :                                valueList.get(1).keySet()) {                                                   }                    } else if (valueList.size() == 1) {                                        for (String cCkey :                                valueList.get(0).keySet()) {                                                   }                    } else if (valueList.size() == 3) {                                                for (String cCkey :                                valueList.get(0).keySet()) {                                                   }                        for (String cSkey :                                valueList.get(1).keySet()) {                                                 }                        for (String cSkey :                                valueList.get(2).keySet()) {                                                   }                    } else {                        return;                    }