How to convert JSONObject to new Map for all its keys using iterator java

11,973

Well u can simply convert the JSON object into a map and then from there u can easily take out the four maps u interested in

here is a simple example

(watch out the code below on a big JSON graph can cause u some problems since it is a recursion based conversion)

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

public class JsonMapConverter {

public static void main(String... x) throws Exception {

    String jsonString = "{\"2016\":{\"12\":{\"20\":{\"19\":{\"DonationTime\":11111111111,\"Donation\":10}}}}}";
    JSONObject json  = new JSONObject(jsonString);
    Map<String,Object> yearMap = toMap(json);

    String year =  yearMap.keySet().iterator().next();
    Map<String,Object> monthMap = ((Map<String, Object>) yearMap.get(year));

    String month = monthMap.keySet().iterator().next();
    Map<String,Object> dayMap = (Map<String, Object>) monthMap.get(month);

    String day = dayMap.keySet().iterator().next();
    Map<String,Object> hourMap = (Map<String, Object>) dayMap.get(day);

    System.out.println(yearMap);
    System.out.println(monthMap);
    System.out.println(dayMap);
    System.out.println(hourMap);
}

public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap<String, Object>();

    Iterator<String> keysItr = object.keys();
    while(keysItr.hasNext()) {
        String key = keysItr.next();
        Object value = object.get(key);

        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }

        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        map.put(key, value);
    }
    return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
    List<Object> list = new ArrayList<Object>();
    for(int i = 0; i < array.length(); i++) {
        Object value = array.get(i);
        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }

        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        list.add(value);
    }
    return list;
  }
}

for JSON to map conversion i use the code from this answer (Convert a JSON String to a HashMap)

the code was written based on the json string, u may adjust the code according to your needs in case of multiple years,month and days presents in the json

Share:
11,973
deadpool
Author by

deadpool

Updated on June 04, 2022

Comments

  • deadpool
    deadpool almost 2 years

    I have a JSONObject

    {"2016":{"12":{"20":{"19":{"DonationTime":11111111111,"Donation":10}}}}}
    

    I want to convert it to new map with each keys

    int i = 0;
    for (Iterator<String> keysItr = object.keySet().iterator(); keysItr.`hasNext(); i++) {
                String key = keysItr.next();
                Object value = object.get(key);
    
                if(value instanceof JSONObject) {
                    value = toMap((JSONObject) value);
    
                    map.put(key, value);
                }
    
            }
    
            SOP(map);   //but here i want to get 4 maps
        }
    

    I want to get 4 maps like

    hourMap[19] = "{"DonationTime":11111111111,"Donation":10}";
    dayMap[20] = "{"19":{"DonationTime":11111111111,"Donation":10}}";
    monthMap[12] = "{"12":{"20":{"19":{"DonationTime":11111111111,"Donation":10}}}";
    yearMap[2016] = "{"12":{"20":{"19":{"DonationTime":11111111111,"Donation":10}}}";
    

    I am using for loop yet i am unable to get incremented value for i.