##JsonObject的输入顺序和输出顺序不一样问题 ###问题原因在于JsonObject的默认实现的是用HashMap,所以我们得把他的构造函数用LinkedHashMap重写
好像是可以修改JsonObject的源码的构造函数,但我没试过,也不建议这么做。
我的方法是继承JsonObject类,然后重写构造方法。
public class MyJSONObject extends JSONObject { private LinkedHashMap<Object, Object> mHashMap; public MyJSONObject() { mHashMap = new LinkedHashMap<Object, Object>(); } @Override public JSONObject put(String name, boolean value) throws JSONException { // TODO Auto-generated method stub return put(name, value); } @Override public JSONObject put(String name, double value) throws JSONException { // TODO Auto-generated method stub return put(name, value); } @Override public JSONObject put(String name, int value) throws JSONException { // TODO Auto-generated method stub return put(name, value); } @Override public JSONObject put(String name, long value) throws JSONException { // TODO Auto-generated method stub return put(name, value); } public JSONObject put(String key, Object value) throws JSONException { if (key == null) { throw new JSONException("Null key."); } if (value != null) { testValidity(value); mHashMap.put(key, value); } else { remove(key); } return this; } public String getKey(String key) throws JSONException{ return mHashMap.get(key).toString(); } public Object remove(String key) { return mHashMap.remove(key); } static void testValidity(Object o) throws JSONException { if (o != null) { if (o instanceof Double) { if (((Double) o).isInfinite() || ((Double) o).isNaN()) { throw new JSONException("JSON does not allow non-finite numbers."); } } else if (o instanceof Float) { if (((Float) o).isInfinite() || ((Float) o).isNaN()) { throw new JSONException("JSON does not allow non-finite numbers."); } } } } public String toString() { try { Iterator