JSON 容易犯的错

義往昔 7月前 ⋅ 639 阅读

JSONObject data = new JSONObject();
data.put("qin",new JSONObject().put("value",123));
System.out.println(data);

// 输出是空{}

data put的值 ,是 对象调用put方法的返回值.... 而不是对象本身

public Object put(String key, Object value) {
return map.put(key, value);
}
        the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>,
* if the implementation supports <tt>null</tt> values.)
map put返回值

所以put("value",123) 原先没这个值,返回null; 

 


全部评论: 0

    我有话说: