Json / JsonArray to HashTable / List in Android



我创建了一个方法"Json to HashTable",反之亦然。我使用HashTable是因为"Java"中没有关联数组。我现在的问题是,当json中有一个数组。这意味着从"Java"的HashTable数组:/根本不起作用,但我认为解决方案是使用"List>"…

我觉得这有点复杂。任何帮助吗?是很难还是我太复杂了?

Json的例子:

{"Config":[{"Name":"method1","Uses":"0","Event":["Start","Play"],"Action":{"Class":"Ads","Options":{"Class":"Webview","Url":"http://test.com/action.php","Time":"10"}}},{"Name":"method2","Uses":"12","Event":["Loading"],"MaxTimes":"5","Options":{"Class":"Ads"}}]}

查看:http://json.parser.online.fr/

我代码:

public Hashtable<?, ?> JSonDecode(String data) {
        Hashtable<String, Object> htJS = new Hashtable<String, Object>();
        try {
            JSONObject objJS = new JSONObject(data);
            Iterator<String> it = objJS.keys();
            String key = null;
            Object value = null;
            while (it.hasNext()) {
                key = it.next();
                value = objJS.get(key);
                if (value instanceof JSONObject) {
                    value = JSonObjectToHashtable(value.toString());
                }
                if (value instanceof JSONArray) {
                    value = JSonArrayToHashtable(value.toString());
                }
                htJS.put((String) key, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            // No valid json
            return null;
        }
        return htJS;
    }
public Hashtable<?, ?> JSonObjectToHashtable(String data) {
        Hashtable<String, Object> htJS = new Hashtable<String, Object>();
        JSONObject objJS;
        try {
            objJS = new JSONObject(data);
            Iterator<String> it = objJS.keys();
            String key = null;
            Object value = null;
            while (it.hasNext()) {
                key = it.next();
                value = objJS.get(key);
                if (value instanceof JSONObject) {
                    value = JSonObjectToHashtable(value.toString());
                }
                htJS.put((String) key, value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return htJS;
    }
public List<Map<String, Object>> JSonArrayToHashtable(String data) {
        List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
        Map<String,Object> entry = new HashMap<String,Object>();
        JSONArray objJSA;
        try {
            objJSA = new JSONArray(data);
            for (int i = 0; i < objJSA.length(); i++) {
                JSONObject objJS = objJSA.getJSONObject(i);
                Iterator<String> it = objJS.keys();
                String key = null;
                Object value = null;
                while (it.hasNext()) {
                    key = it.next();
                    value = objJS.get(key);
                    if (value instanceof JSONObject) {
                        value = JSonObjectToHashtable(value.toString());
                    }
                    entry.put((String) key, value);
                }
                listMap.add(entry);
                entry = new HashMap<String,Object>();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return listMap;
    }

Map (Hashtable) API类似于JSONObject API。没有必要将JSONObject转换为Map,除非您的应用程序始终使用Map。

如果需要将JSONObject转换为Map, Map可以是Map<String, Object>类型,其中Object可以是以下类型之一:

    字符串
  • 原语(整数、浮点等)
  • 上述树类型的集合(Array、List等)

最新更新