##JsonObject的输入顺序和输出顺序不一样问题 ###问题原因在于JsonObject的默认实现的是用HashMap,所以我们得把他的构造函数用LinkedHashMap重写
-
好像是可以修改JsonObject的源码的构造函数,但我没试过,也不建议这么做。
-
我的方法是继承JsonObject类,然后重写构造方法。
public class MyJSONObject extends JSONObject { private LinkedHashMap<Object, Object> mHashMap;
1public MyJSONObject() { 2 mHashMap = new LinkedHashMap<Object, Object>(); 3} 4 5@Override 6public JSONObject put(String name, boolean value) throws JSONException { 7 // TODO Auto-generated method stub 8 return put(name, value); 9} 10 11@Override 12public JSONObject put(String name, double value) throws JSONException { 13 // TODO Auto-generated method stub 14 return put(name, value); 15} 16 17@Override 18public JSONObject put(String name, int value) throws JSONException { 19 // TODO Auto-generated method stub 20 return put(name, value); 21} 22 23@Override 24public JSONObject put(String name, long value) throws JSONException { 25 // TODO Auto-generated method stub 26 return put(name, value); 27} 28 29public JSONObject put(String key, Object value) throws JSONException { 30 if (key == null) { 31 throw new JSONException("Null key."); 32 } 33 if (value != null) { 34 testValidity(value); 35 mHashMap.put(key, value); 36 } else { 37 remove(key); 38 } 39 return this; 40} 41 42public String getKey(String key) throws JSONException{ 43 return mHashMap.get(key).toString(); 44} 45 46public Object remove(String key) { 47 return mHashMap.remove(key); 48} 49 50static void testValidity(Object o) throws JSONException { 51 if (o != null) { 52 if (o instanceof Double) { 53 if (((Double) o).isInfinite() || ((Double) o).isNaN()) { 54 throw new JSONException("JSON does not allow non-finite numbers."); 55 } 56 } else if (o instanceof Float) { 57 if (((Float) o).isInfinite() || ((Float) o).isNaN()) { 58 throw new JSONException("JSON does not allow non-finite numbers."); 59 } 60 } 61 } 62} 63 64public String toString() { 65 try { 66 Iterator<Object> keys = mHashMap.keySet().iterator(); 67 StringBuffer sb = new StringBuffer("{"); 68 69 while (keys.hasNext()) { 70 if (sb.length() > 1) { 71 sb.append(','); 72 } 73 Object o = keys.next(); 74 sb.append(quote(o.toString())); 75 sb.append(':'); 76 sb.append(valueToString(mHashMap.get(o))); 77 } 78 sb.append('}'); 79 return sb.toString(); 80 } catch (Exception e) { 81 return null; 82 } 83} 84 85static String valueToString(Object value) throws JSONException { 86 if (value == null || value.equals(null)) { 87 return "null"; 88 } 89 if (value instanceof JSONStringer) { 90 Object o; 91 try { 92 o = ((JSONStringer) value).toString(); 93 } catch (Exception e) { 94 throw new JSONException(e.getMessage()); 95 } 96 if (o instanceof String) { 97 return (String) o; 98 } 99 throw new JSONException("Bad value from toJSONString: " + o); 100 } 101 if (value instanceof Number) { 102 return numberToString((Number) value); 103 } 104 if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) { 105 return value.toString(); 106 } 107 if (value instanceof Map) { 108 return new JSONObject((Map) value).toString(); 109 } 110 if (value instanceof Collection) { 111 return new JSONArray((Collection) value).toString(); 112 } 113 return quote(value.toString()); 114}}