1

Code

    //Just assume this Json is perfectly fine.  Taken from a web services
    this.jsonData = [ { "mobile": "12345", "msg": "Test Message", "id": "28991", "name": "Test User", "senttime": "2019-07-05 14:36:24", "company": "My Company" }]

    public static HashMap<String, String> map_id_to_mobile = new HashMap<String, String>();
    public static HashMap<String, String> map_id_to_name = new HashMap<String, String>();
    public static HashMap<String, String> map_id_to_message = new HashMap<String, String>();
    public static HashMap<String, String> map_id_to_company = new HashMap<String, String>();
    JSONArray JA = new JSONArray(this.jsonData);
    int i = 0;
    while (i < JA.length()) {
        JSONObject JO = (JSONObject) JA.get(i);
        map_id_to_mobile.put(JO.get("id").toString(), JO.get("mobile").toString() );
        map_id_to_name.put(JO.get("id").toString(), JO.get("name").toString() );
        map_id_to_message.put(JO.get("id").toString(), JO.get("msg").toString() );
        map_id_to_company.put(JO.get("id").toString(), JO.get("company").toString() );
    }

I am not satisfied with this code because of the the four different arrays. I just wanted to use a php like multi-dimensional arrays where i could just do the following.

smsData[JO.get('id')] = array('mobile'=> JO.get('mobile).toString(), 'msg' => JO.get('msg').toString(), 'company' => JO.get('company').toString();

The reason for this is the ease of accessing the data when the array becomes more multi dimensional. Any comments? Any reference with similar idea pls let me know... been scouring stackoverflow but not satisfied with the answers... Please help.

4
  • use bean class instead of hashmaps Commented Jul 24, 2019 at 6:26
  • I dont understand what you want. Can you post a dummy json like you want at the end? Commented Jul 24, 2019 at 7:48
  • This is how i want to get the values from the array. | system.out (mydata[28991]['mobile']) //returns 12345 | | system.out (mydata[28991]['name']) //returns Test User | | system.out (mydata[28991]['msg']) //returns Test Message | I hope my intention is clear enough. Commented Jul 24, 2019 at 7:58
  • hey, i posted an answer. check it Commented Jul 24, 2019 at 10:34

1 Answer 1

1
public  Map<String, HashMap<String, String>> map = new HashMap<String, HashMap<String, String>>();


JSONArray JA = new JSONArray(this.jsonData);
    int i = 0;
    while (i < JA.length()) {
        JSONObject JO = (JSONObject) JA.get(i);
        Map<String, String> innerMap = new HashMap<String, String>();
        innerMap.put("mobile", JO.get("mobile").toString());
        innerMap.put("msg", JO.get("msg").toString());
        innerMap.put("company", JO.get("company").toString());
        innerMap.put("name", JO.get("name").toString());

        map.put(JO.get("id").toString(), innerMap);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

map.put(JO.get("id").toString(), (HashMap<String, String>) innerMap); Need to somehow cast the inner map, but can easily be fixed. Thank you for this solution. It works for me as of 25 July, 2019.
map.get("28991" ).get("mobile") //returns 12345 . | thanks my friend.
Glad to hear. If you solve problem maybe you can mark my answer as right :)

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.