Android Open Source - Icinga-Mobile Json Helper

From Project

Back to project page Icinga-Mobile.


The source code is released under:

GNU General Public License

If you think the Android project Icinga-Mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mhst.dreamteam.IcingaClient.Json;
// w  w w. ja  v a  2s .c o m

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;

public class JsonHelper {
    public static Object toJSON(Object object) throws JSONException {
        if (object instanceof Map) {
            JSONObject json = new JSONObject();
            Map map = (Map) object;
            for (Object key : map.keySet()) {
                if(key == null)
                    if(map.get(key) == null)
                        json.put(key.toString(), JSONObject.NULL);
                        json.put(key.toString(), toJSON(map.get(key)));
            return json;
        } else if (object instanceof Iterable) {
            JSONArray json = new JSONArray();
            for (Object value : ((Iterable)object)) {
            return json;
        } else {
            return object;

    public static boolean isEmptyObject(JSONObject object) {
        return object.names() == null;

    public static Map<String, Object> getMap(JSONObject object, String key) throws JSONException {
        return toMap(object.getJSONObject(key));

    public static Map<String, Object> toMap(JSONObject object) throws JSONException {
        Map<String, Object> map = new HashMap<String, Object>();
        Iterator keys = object.keys();
        while (keys.hasNext()) {
            String key = (String);
            map.put(key, fromJson(object.get(key)));
        return map;

    public static ArrayList toArrayList(JSONArray array) throws JSONException {
        ArrayList<Object> list = new ArrayList<Object>();
        for (int i = 0; i < array.length(); i++) {
        return list;

    private static Object fromJson(Object json) throws JSONException {
        if (json == JSONObject.NULL) {
            return null;
        } else if (json instanceof JSONObject) {
            return toMap((JSONObject) json);
        } else if (json instanceof JSONArray) {
            return toArrayList((JSONArray) json);
        } else {
            return json;

Java Source Code List