Java - net.sf.json 之 put、accumulate、element 实践与疑问

发布时间:2019-11-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Java - net.sf.json 之 put、accumulate、element 实践与疑问脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
net.sf.json

net.sf.json 需要的 jar


注意版本,个别版本之间会冲突。
 

Java 代码:

package com.code.ggsddu;   import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject;   import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;   public class testJSON {     public static void main(String[] args) {         /**          * public Object put(Object key, Object value)          * 将value映射到key下          * 如果此JSONObject对象之前存在一个value在这个key下,那么当前的value会替换掉之前的value          */         JSONObject jsonObject = new JSONObject();         jsonObject.put("one", "First");         // jsonObject: {"one":"first"}         System.out.PRintln("jsonObject: " + jsonObject.toString());           jsonObject.put("two", "second");         // jsonObject: {"one":"first","two":"second"}         System.out.println("jsonObject: " + jsonObject.toString());           jsonObject.put("two", "cover");         // jsonObject: {"one":"first","two":"cover"}         System.out.println("jsonObject: " + jsonObject.toString());           jsonObject.put("one", null);// value为null的话,直接移除key         // jsonObject: {"two":"cover"}         System.out.println("jsonObject: " + jsonObject.toString());           /**          * public JSONObject accumulate(String key, Object value)          * 累积value到这个key下          * 1.如果当前已经存在一个value在这个key下,那么会有一个JSONArray将存储在这个key下来保存所有累积的value          * 2.如果已经存在一个JSONArray,那么当前的value就会添加到这个JSONArray中          */         JSONObject jsonObj = new JSONObject();         jsonObj.accumulate("Servers", null);// 允许value为null         jsonObj.accumulate("Servers", "Tomcat");         jsonObj.put("Codes", "Java");         jsonObj.accumulate("Codes", "JavaScript");         // jsonObj: {"Servers":[null,"Tomcat"],"Codes":["Java","JavaScript"]}         System.out.println("jsonObj: " + jsonObj.toString());           /**          * public JSONObject element(String key, Object value)          */         JSONObject object = new JSONObject();         object.element("price", "500");         object.element("price", "1000");         // object: {"price":"1000"} 疑问: 这和put有何区别??? 说好的会调用accumulate呢???         System.out.println("object: " + object.toString());     } }

 

控制台打印结果:

jsonObject: {"one":"first"} jsonObject: {"one":"first","two":"second"} jsonObject: {"one":"first","two":"cover"} jsonObject: {"two":"cover"} jsonObj: {"Servers":[null,"Tomcat"],"Codes":["Java","JavaScript"]} object: {"price":"1000"}

 

疑问:

net.sf.json.element :
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. If there is a previous value assigned to the key, it will call accumulate.
 
大意为:将键/值对放到这个 JSONObject 对象里面。如果当前 value 为 null,那么如果这个 key 存在的话,这个 key 就会移除掉。如果这个 key 之前有 value ,那么此方法会调用 accumulate 方法。

亲测element ,却不尽然,结果并不是如上的预期的效果。为什么呢?

脚本宝典总结

以上是脚本宝典为你收集整理的Java - net.sf.json 之 put、accumulate、element 实践与疑问全部内容,希望文章能够帮你解决Java - net.sf.json 之 put、accumulate、element 实践与疑问所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。