Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert json into map in java example

public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
    Map<String, Object> retMap = new HashMap<String, Object>();

    if(json != JSONObject.NULL) {
        retMap = toMap(json);
    }
    return retMap;
}

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;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: closures in javascript 
Javascript :: js push array into array 
Javascript :: what is adapter.js 
Javascript :: jsx loop array 
Javascript :: parseint javascript 
Javascript :: electron get printer list 
Javascript :: jquery get custom attribute 
Javascript :: mac os chrome opne debug new tab 
Javascript :: checking scroll position with js 
Javascript :: vue 3 hooks 
Javascript :: 7) Change cursor:pointer at checkboxes in java script 
Javascript :: How to set up ejs 
Javascript :: convert date to unix timestamp javascript 
Javascript :: nested function javascript 
Javascript :: javascript array contains 
Javascript :: backbone js cdn 
Javascript :: price range slider bootstrap 4 
Javascript :: Add an item to the beginning of an Array 
Javascript :: string match method 
Javascript :: let var diferencia 
Javascript :: cordova delete cache 
Javascript :: react iterate over map 
Javascript :: type checking js vscode 
Javascript :: sort numbers in javascript 
Javascript :: axios data fetch 
Javascript :: parseint js 
Javascript :: anjular js 
Javascript :: Redux thunk and react redux 
Javascript :: set body id js 
Javascript :: javascript how to do else if 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =