在Android中解析Json时遇到问题

时间:2019-01-03 05:26:53

标签: android json

我的服务器端程序员在其服务器上为wp添加了CoCart api插件。我从服务器那里得到了休养:

{
"0c6e4b403b2381e055e3afb9c9e82192": {
    "key": "0c6e4b403b2381e055e3afb9c9e82192",
    "product_id": 36187,
    "variation_id": 36188,
    "variation": {
        "attribute_pa_colour": "black-de",
        "attribute_pa_size": "m"
    },
    "quantity": 1,
    "data_hash": "8efa7dad6a9c192be37892171605600f",
    "line_tax_data": {
        "subtotal": [],
        "total": []
    },
    "line_subtotal": 1250,
    "line_subtotal_tax": 0,
    "line_total": 1250,
    "line_tax": 0,
    "data": {},
    "product_name": "Женское пальто - Black"
},
"7c9c27e24ba60230327a8d915f71ae70": {
    "key": "7c9c27e24ba60230327a8d915f71ae70",
    "product_id": 36169,
    "variation_id": 36170,
    "variation": {
        "attribute_pa_colour": "green",
        "attribute_pa_size": "m"
    },
    "quantity": 2,
    "data_hash": "673b79f69d443c0e3321faa0cf145f53",
    "line_tax_data": {
        "subtotal": [],
        "total": []
    },
    "line_subtotal": 3200,
    "line_subtotal_tax": 0,
    "line_total": 3200,
    "line_tax": 0,
    "data": {},
    "product_name": "Женское пальто - Green"
}

}

不幸的是,我对此一无所知。我使用Java Android的JSONObject,但是如果每次更改键,如何获取对象。正是产品的关键。谢谢)

3 个答案:

答案 0 :(得分:1)

您可以尝试一下,JSONObject为您提供了一个keys()函数,该函数返回Iterator个动态键(在您的json中),您可以将其与哈希映射一起用于获取和存储动态数据-

try {
        JSONObject productJson = new JSONObject(productJsonString);
        Iterator keys = productJson.keys();


        HashMap<String, List<String>> dynamicList = new HashMap<>();

        while (keys.hasNext()) {
            String currentDynamicKey = (String) keys.next();

            JSONObject currentArrayValue = productJson.getJSONObject(currentDynamicKey);
            ArrayList<CustomObjectWithFieldsInsideDynamicKey> currentArrayData = new ArrayList<>();
            for (int i = 0; i < currentArrayValue.length(); i++) {
                currentArrayData.add(currentArrayValue.getString(i));
            }

            dynamicList.put(currentDynamicKey, currentArrayData);
        }

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }

希望这会有所帮助!

答案 1 :(得分:0)

JSONObject中有一个keys()。这将返回该JSONObject中可用的密钥。以下是获取密钥的给定片段。获得密钥后,可以调用该密钥的getJSONObject或getJSONArray。

String jsonString = "{\"a\":{},\"b\":{}}";
try{
    JSONObject json = new JSONObject(jsonString);
    Iterator<String> keys = json.keys();
    while(keys.hasNext()) {
        System.out.println(keys.next());
    }
}catch(Exception e){
    e.printStackTrace();
}

答案 2 :(得分:0)

这是我的Java代码,用于从JSONArray获取密钥。

private JSONArray getHeaders(JSONArray data) {
        JSONArray jsonArray = new JSONArray();
        if (data.length() > 0) {
            try {
                Iterator<String> iterator = data.getJSONObject(0).keys();
                int i = 1;
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("keyFromObject", key);
                    jsonArray.put(jsonObject);
                    i++;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonArray;
    }

参考链接:-doc