2012-02-09 77 views
0

我正面臨創建Json的問題,這實際上是一個複雜的問題,我只需要通過HashMap創建它。我實際上正在尋找一些遞歸函數,這可能是我的問題的最佳解決方案。如何在Android中使用HashMap創建複雜的JSON?

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" 
    } 
} 

任何想法?

+0

到目前爲止你試過了什麼? – Selvin 2012-02-09 12:12:11

回答

0

我們使用GSON進行對象/ JSON轉換。這裏有一個鏈接以獲得更多信息:GSON

+0

那麼斯科特..使用Gson是一個很好的選擇,其實我blv是最好的因爲它最快......但笏Jens回答是我正在尋找的東西.. – user983947 2012-02-10 05:14:38

3

你會做這樣的事情:

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" 
     } 
    } 
} 
+0

謝謝Jens .....我在做同樣的事情,但在這方面面臨着一些問題。 Nyways ..我從我貼的代碼中得到了我的錯誤。 – user983947 2012-02-10 05:11:27

相關問題