0

I have a problem with putting JSONArray data to Map<String, Object>.

JSONArray contains array with value and key and I want to map these values (value as Object and key as String)

[
    {
        "value": {
            "gamesPlayed": 0,
            "name": "Arti",
            "elo": 2000,
            "uuid": "2ae8d022-e0f4-4502-8e0e-1874997543e3",
            "email": "[email protected]"
        },
        "key": "03a2452c-9d6b-47f5-9616-9a6833312762"
    }, {
        "value": {
            "gamesPlayed": 0,
            "name": "Kamil",
            "elo": 2000,
            "uuid": "1bb43d73-3f94-40fc-a680-99f4a9304001",
            "email": "[email protected]"
        },
        "key": "8526db7c-6930-45bf-9ae1-fb93e97ff4ba"
    }
]

do you have any simple solutions to do this ?

3
  • In that case lets get back to your question. What is JSONArray? From which package/library it comes from (to create code we need to know what methods/API are available in that class)? Commented Jul 8, 2022 at 13:44
  • Anyway I suspect that solution you are looking for is something along Map<String, Object> map = new HashMap<>(); for (int i = 0; i<jsonArray.size(); i++) { JSONObject obj = jsonArray.getJSONObject(i); map.put(obj.getString("key", obj.getJSONObject("value")); }. Commented Jul 8, 2022 at 13:46
  • JSONArray comes from org.json, just like JSONObject. Commented Jul 8, 2022 at 13:53

1 Answer 1

1

JSON-lib is a java library for transforming beans, maps, collections, java arrays and XML to JSON and back again to beans and DynaBeans.

https://kordamp.org/json-lib/

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

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.