【问题标题】:How to create a complex JSON using HashMap in android?如何在android中使用HashMap创建一个复杂的JSON?
【发布时间】:2012-03-01 21:20:00
【问题描述】:

我在创建 Json 时遇到问题,这实际上是一个复杂的 Json,我只需要通过 HashMap 来创建它。我实际上是在寻找一些可能是解决我的问题的最佳方法的递归函数。

我需要创建的 JSON 看起来像..

{"pkt":{
    "data2":{"z":"3", "y":"2", "x":"1"},
    "data3":{"n":"3", "l":"1", "m":"2"},
    "mid":"1328779096525",
    "data1":{"b":"2", "c":"3", "a":"1"},
    "msg":"10012"
    }
}

有什么想法吗??

【问题讨论】:

    标签: android json hashmap


    【解决方案1】:

    你会这样做:

    public void toJSON(Map<?, ?> map, JSONStringer stringer) throws JSONException {
        stringer.object();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            stringer.key(String.valueOf(entry.getKey()));
            toJSONValue(entry.getValue(), stringer);
         }
        stringer.endObject();
    }
    
    public void toJSONValue(Object value, JSONStringer stringer) throws JSONException {
        if (value == null) {
            stringer.value(null);
        } else if (value instanceof Collection) {
            toJSON((Collection<?>) value, stringer);
        } else if (value instanceof Map) {
            toJSON((Map<?, ?>) value, stringer);
        } else if (value.getClass().isArray()) {
            if (value.getClass().getComponentType().isPrimitive()) {
                stringer.array();
                if (value instanceof byte[]) {
                    for (byte b : (byte[]) value) {
                        stringer.value(b);
                    }
                } else if (value instanceof short[]) {
                    for (short s : (short[]) value) {
                        stringer.value(s);
                    }
                } else if (value instanceof int[]) {
                    for (int i : (int[]) value) {
                        stringer.value(i);
                    }
                } else if (value instanceof float[]) {
                    for (float f : (float[]) value) {
                        stringer.value(f);
                    }
                } else if (value instanceof double[]) {
                    for (double d : (double[]) value) {
                        stringer.value(d);
                    }
                } else if (value instanceof char[]) {
                    for (char c : (char[]) value) {
                        stringer.value(c);
                    }
                } else if (value instanceof boolean[]) {
                    for (boolean b : (boolean[]) value) {
                        stringer.value(b);
                    }
                }
                stringer.endArray();
            } else {
                toJSON((Object[]) value, stringer);
            }
        } else {
            stringer.value(value);
        }
    }
    
    public void toJSON(Object[] array, JSONStringer stringer) throws JSONException {
        stringer.array();
        for (Object value : array) {
            toJSONValue(value, stringer);
        }
        stringer.endArray();
    }
    
    public void toJSON(Collection<?> collection, JSONStringer stringer) throws JSONException {
        stringer.array();
        for (Object value : collection) {
            toJSONValue(value, stringer);
        }
        stringer.endArray();
    }
    

    构建你给出的例子:

        // Using a variety of maps since all should work..
        HashMap<String, Object> pkt = new HashMap<String, Object>();
    
        LinkedHashMap<String, String> data1 = new LinkedHashMap<String, String>();
        data1.put("b", "2");
        data1.put("c", "3");
        data1.put("a", "1");
    
        LinkedHashMap<String, String> data2 = new LinkedHashMap<String, String>();
        data2.put("z", "3");
        data2.put("y", "2");
        data2.put("x", "1");
    
        TreeMap<String, Object> data3 = new TreeMap<String, Object>();
        data3.put("z", "3");
        data3.put("y", "2");
        data3.put("x", "1");
    
        pkt.put("data2", data2);
        pkt.put("data3", data3);
        pkt.put("mid", "1328779096525");
        pkt.put("data1", data1);
        pkt.put("msg", "10012");
        try {
            JSONStringer stringer = new JSONStringer();
            stringer.object();
            stringer.key("pkt");
            toJSON(pkt, stringer);
            stringer.endObject();
            System.out.println(stringer.toString());
        } catch (JSONException e) {
            // Time for some error-handling
        }
    

    这将导致(格式化以供查看):

    {
       "pkt":{
          "data2":{
             "z":"3",
             "y":"2",
             "x":"1"
          },
          "mid":"1328779096525",
          "data3":{
             "x":"1",
             "y":"2",
             "z":"3"
          },
          "msg":"10012",
          "data1":{
             "b":"2",
             "c":"3",
             "a":"1"
          }
       }
    }
    

    【讨论】:

    • 谢谢 Jens .....我也在做同样的事情,但在这方面遇到了一些问题。 Nyways..我从你发布的代码中弄错了。
    【解决方案2】:

    我们使用 GSON 进行对象/JSON 转换。以下是更多信息的链接:GSON

    【讨论】:

    • 好吧 Scott.. 使用 Gson 是一个不错的选择,实际上我 blv 是最好的,因为它是最快的...但是 wat Jens 回答是我正在寻找的东西..
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多