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,却不尽然,结果并不是如上的预期的效果。为什么呢?

原文地址:https://www.cnblogs.com/ikoo4396/p/8214110.html

时间: 2024-08-30 10:48:06

Java - net.sf.json 之 put、accumulate、element 实践与疑问的相关文章

net.sf.json - put、accumulate、element

net.sf.json 需要的 jar: 注意版本,部分版本之间会冲突. package com.ikoo; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class TestJSON { public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); JS

json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不给我们报错,很郁闷, 特别是ie, 有些问题, 得借助FireFox的返回结果分析. 当然, FireFox有时也没报错. 异常栈: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException at net.sf.js

含有Date类型属性的实体类转化为JSONArray时报net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

当我们通过session传递数据的时候我通常是: 直接在DAO里从数据库取出含Date类型的数据,而且通常不会将java.sql.Date转为java.util.Date. 这时候前台和后台都不会有任何问题,所以我们以为这样是正常的操作. 但是当我们用JSON传递数据的时候就会发现控制台会报一下错误信息: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 这时候我们可以再DAO里将java.sql.Dat

java中使用net.sf.json对json进行解析

net.sf.json依赖的包很多. 有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar /** * 从一个JSON 对象字符格式中得到一个java对象,形如: * {"id" : idValue, "name" : nameValue, "aBean" : {&qu

JSON中 net.sf.json.JSONException: java.lang.NoSuchMethodException异常

在json对象和java对象转换时 String s = "{'name':'name1','pwd':'pwd1'}"; Person p = (Person)JSONObject.toBean(JSONObject.fromObject(s), Person.class); System.out.println(p.getPwd()); 上面代码中出现以下异常: net.sf.json.JSONException: java.lang.NoSuchMethodException [

json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

1 //存储Product对象的集合是从,Product是从mysql数据库中查询并添加的 2 ArrayList<Product> list = new ArrayList<Product>(); 3 4 //设置响应对象编码 5 response.setCharacterEncoding("utf-8"); 6 response.setContentType("text/html;charset=utf-8"); 7 8 //将list集

ajax请求json数据异常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause

ajax请求json数据异常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause 1.异常原因:所请求的json数据中包含java.util.date数据类型,但是在后台并没有将其格式转换 2.解决方法:添加工具类DateJsonValueProcessor import java.text.SimpleDateFormat; imp

java.lang.ClassNotFoundException: net.sf.json.JSONObject

先去检查依赖包的问题 使用net.sf.json需要导入的包 如果jar 文件都存在. 右键项目选择Build path 在develoyment Assembly 添加 maven dependencies 网友回答1: 我已经导入了json的jar包及其需要的依赖包,过程是:右键项目选择Build path –> config build path –>add extenal jar加入以上所需要的jar.此时发现在servlet中写的关于JSONObject的代码不发生错误了,然后运行程

使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject

先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找不到json相关的包, 我就手动将json-lib-2.3-jdk15.jar这个包引入,但是还是报同样的错误, 上网搜了下,有人说还需要把这个包放到对应TOMCAT/LIB目录下, 于是照做,终于不报这个错误了,但是报了其他错误: org.apache.commons.lang.exception