/** * Created by wangmeng on 15/10/2016. */ publicclassJsonTest{ publicstaticvoidmain(String[] args){ JSONObject jsonObject=new JSONObject(); jsonObject.put("name","matt"); jsonObject.put("age",24); jsonObject.put("double",10/0.0); System.out.println(jsonObject.toString()); } }
程序结果真是报错,报错内容如下:
1 2 3 4 5 6 7 8 9 10
Exception in thread "main" org.json.JSONException: JSON does not allow non-finite numbers. at org.json.JSONObject.testValidity(JSONObject.java:1578) at org.json.JSONObject.put(JSONObject.java:1291) at org.json.JSONObject.put(JSONObject.java:1220) at com.meituan.kafka.json.JsonTest.main(JsonTest.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
/** * Put a key/value pair in the JSONObject. If the value is null, then the * key will be removed from the JSONObject if it is present. * * @param key * A key string. * @param value * An object which is the value. It should be of one of these * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, * String, or the JSONObject.NULL object. * @return this. * @throws JSONException * If the value is non-finite number or if the key is null. */ public JSONObject put(String key, Object value)throws JSONException { if (key == null) { thrownew NullPointerException("Null key."); } if (value != null) { testValidity(value); this.map.put(key, value); } else { this.remove(key); } returnthis; }
/** * Throw an exception if the object is a NaN or infinite number. * * @param o * The object to test. * @throws JSONException * If o is a non-finite number. */ publicstaticvoidtestValidity(Object o)throws JSONException { if (o != null) { if (o instanceof Double) { if (((Double) o).isInfinite() || ((Double) o).isNaN()) { thrownew JSONException( "JSON does not allow non-finite numbers."); } } elseif (o instanceof Float) { if (((Float) o).isInfinite() || ((Float) o).isNaN()) { thrownew JSONException( "JSON does not allow non-finite numbers."); } } } }
查到这里,找到了具体的原因,对 Double 类型的对象,其值不能为 NAN 和 INFINITY,下面再看一下这个两个值在 java 中是如何定义的,对于 double 型的变量
publicfinalclassDoubleextendsNumberimplementsComparable<Double> { /** * A constant holding the positive infinity of type * {@code double}. It is equal to the value returned by * {@code Double.longBitsToDouble(0x7ff0000000000000L)}. */ publicstaticfinaldouble POSITIVE_INFINITY = 1.0 / 0.0;
/** * A constant holding the negative infinity of type * {@code double}. It is equal to the value returned by * {@code Double.longBitsToDouble(0xfff0000000000000L)}. */ publicstaticfinaldouble NEGATIVE_INFINITY = -1.0 / 0.0;
/** * A constant holding a Not-a-Number (NaN) value of type * {@code double}. It is equivalent to the value returned by * {@code Double.longBitsToDouble(0x7ff8000000000000L)}. */ publicstaticfinaldouble NaN = 0.0d / 0.0; ..... }