zoukankan      html  css  js  c++  java
  • Java

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

  • 相关阅读:
    Unity3D移动端海水的实时绘制
    NGUI 3.x 深度管理及渲染优化
    【入门】从学生到成熟:游戏模块设计起步之抽象思维 (转)
    正弦波近似 http://blog.csdn.net/ring0hx/article/details/44492415
    Stack 栈 ----Queue 队列
    ORM
    CBV&FBV
    Django路由系统
    CRM
    深浅拷贝
  • 原文地址:https://www.cnblogs.com/ikoo4396/p/8214110.html
Copyright © 2011-2022 走看看