2012-02-09 3 views
0

나는 실제로 복잡한 하나의 Json을 만드는 문제에 직면하고있다. 나는 HashMap을 통해서만 그것을 생성해야한다. 나는 내 문제에 대한 최선의 해결책이 될 수있는 재귀 함수를 실제로 찾고 있었다. 내가 ..android에서 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" 
    } 
} 

어떤 아이디어 같은 외모를 만들 필요가

JSON?

+0

지금까지 시도한 것은 무엇입니까? – Selvin

답변

0

우리는 객체/JSON 변환에 GSON을 사용하고 있습니다.

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(); 
} 

당신이 준 예를 구성하려면 : GSON

+0

음 Scott ... Gson을 사용하는 것은 좋은 대안입니다. 실제로 blv는 가장 빠른 것입니다 ...하지만 Jens는 내가 찾던 것이 었습니다. – user983947

3

당신은 같은 것을 할 거라고 : 여기에 대한 추가 정보에 대한 링크입니다 초래

// 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 .. 나는 코드 u에서 내 실수를했습니다. – user983947

관련 문제