0

我创建了一个方法“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;
    }
4

1 回答 1

0

Map (Hashtable) API 类似于 JSONObject API。除非您的应用程序始终使用 Maps,否则实际上不需要将 JSONObject 转换为 Map。

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

  • 细绳
  • 基元(整数、浮点数等)
  • 地图
  • 上述树类型的集合(数组、列表等)
于 2013-06-02T21:53:52.490 回答