zoukankan      html  css  js  c++  java
  • Json相关内容

    一、

    导入包:net.sf.json.JSONObject

    代码

    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());
        }
    }
    View Code

    结果如下

    1 jsonObject: {"one":"first"}
    2 jsonObject: {"one":"first","two":"second"}
    3 jsonObject: {"one":"first","two":"cover"}
    4 jsonObject: {"two":"cover"}
    5 jsonObj: {"Servers":[null,"Tomcat"],"Codes":["Java","JavaScript"]}
    6 object: {"price":"1000"}
    View Code
  • 相关阅读:
    HDU 3853 LOOPS:期望dp【网格型】
    SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
    BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】
    BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
    BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】
    codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
    poj1964最大子矩阵 (单调栈加枚举)
    poj3111 选取物品(二分+贪心)
    codeforces-777E Hanoi Factory (栈+贪心)
    poj3040 发工资(贪心)
  • 原文地址:https://www.cnblogs.com/txppp/p/10895796.html
Copyright © 2011-2022 走看看