JSON 容易犯的错

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;

评论