android json map解析

时间:2024-03-27 19:11:26

android json map解析

返回类似上面数据格式时:

List<MoiveBean> moiveBeanList = new ArrayList<>();
Map<String, Object> map = JsonUtil.getMapForJson(o.toString());
Set<String> set = map.keySet();


map.remove("total_count");

List<Integer> keyLsit = new ArrayList<>();
for (String s : set) {
    keyLsit.add(Integer.parseInt(s));
}
Collections.sort(keyLsit);

for (Integer i : keyLsit) {
    MoiveBean moiveBean = JSONObject.parseObject(map.get(i + "").toString(), MoiveBean.class);
    LogUtil.e(TAG, moiveBean.toString());
    moiveBeanList.add(moiveBean);
}


JsonUtil.class:


/**
 * Json 转成 Map<>
 *
 * @param jsonStr
 * @return
 */
public static Map<String, Object> getMapForJson(String jsonStr) {
    JSONObject jsonObject;
    try {
        jsonObject = new JSONObject(jsonStr);

        Iterator<String> keyIter = jsonObject.keys();
        String key;
        Object value;
        Map<String, Object> valueMap = new HashMap<String, Object>();
        while (keyIter.hasNext()) {
            key = keyIter.next();
            value = jsonObject.get(key);
            valueMap.put(key, value);
        }
        return valueMap;
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Log.e("StringUtil", e.toString());
    }
    return null;
}