Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;

public class Main {
    private static final String TAG = "CXXUtil";

    @SuppressWarnings("unchecked")
    public static Map<String, String> convertFromJSONToMap(String str) {
        Map<String, String> map = new HashMap<String, String>();
        if (isNullOrEmpty(str) == false) {
            try {
                JSONObject json = new JSONObject(str);
                Iterator<String> keys = json.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    String val = json.getString(key);
                    map.put(key, val);
                }
            } catch (JSONException e) {
                Log.e(TAG, "convertFromCXX to org.json.JSONArray " + str);
            }
        }
        return map;
    }

    @SuppressWarnings("rawtypes")
    protected static boolean isNullOrEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String) {
            if (((String) obj).length() == 0) {
                return true;
            }
        }
        if (obj instanceof Collection) {
            if (((Collection) obj).size() == 0) {
                return true;
            }
        }
        if (obj instanceof Map) {
            if (((Map) obj).size() == 0) {
                return true;
            }
        }
        if (isArray(obj)) {
            if (Array.getLength(obj) == 0) {
                return true;
            }
        }
        return false;
    }

    protected static boolean isArray(Object obj) {
        if (obj.getClass().isArray()) {
            return true;
        }
        return false;
    }
}