0

My server-side programmer added CoCart api plugin for wp on his server. I got respose from server:

{
"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"
}

}

Unfortunately, I have no any idea about what should I do with this. I use Java Android's JSONObject, but how can I get the object if keys changing every time. Exactly, keys of products. Thanks)

4
  • No it's not the answer Commented Jan 3, 2019 at 5:34
  • But what if I'll create some array, like {JsonArray array=new JsonArray()} Commented Jan 3, 2019 at 5:39
  • And put there that json Commented Jan 3, 2019 at 5:40
  • You can but you have to go through to the array position. Try it Commented Jan 3, 2019 at 5:48

3 Answers 3

1

You can try this, JSONObject gives you a keys() function which returns an Iterator of dynamic keys (in your json), you can use that along with a hashmap for fetching and storing dynamic data -

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();
    }

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Let me know if you need any more help with this!
0

There is a keys() available in JSONObject. which will return the key available in that JSONObject. Below is a given snippet to get the keys. Once you get the keys you can call getJSONObject or getJSONArray for that key.

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();
}

1 Comment

Thank you for your response, unfortunately I can accept only one answer(( However, thank you))))
0

This is my Java code to get keys from 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;
    }

Reference link :- Json Object - Getting the Key and the Value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.