Java-net.sf.json's put, accumulate, element practice and questions

Source: Internet
Author: User

Net.sf.json

net.sf.jsonNeed for jar


Note that versions are conflicting between individual versions.

Java Code:
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, OBJ ECT value) * Map value to key * If there is a value under this key before this Jsonobject object, then the current value will replace the previous value */JS        Onobject jsonobject = new Jsonobject ();        Jsonobject.put ("One", "first");         Jsonobject: {"One": "First"} System.out.println ("Jsonobject:" + jsonobject.tostring ());        Jsonobject.put ("Both", "second");         Jsonobject: {"One": "First", "One": "Second"} System.out.println ("Jsonobject:" + jsonobject.tostring ());        Jsonobject.put ("Both", "cover");         Jsonobject: {"One": "First", "One": "Cover"} System.out.println ("Jsonobject:" + jsonobject.tostring ()); Jsonobject.put ("one", null);//value is Null, directly remove key//Jsonobject: {"One": "Cover"} System.out.println ("Jsonobject:" + jsonobject.tostring ()); /** * Public jsonobject accumulate (String key, Object value) * Accumulate value under this key * 1. If there is currently a V        Alue under this key, then there will be a jsonarray stored in this key down to save all accumulated value * 2. If a jsonarray already exists, the current value is added to this jsonarray */        Jsonobject jsonobj = new Jsonobject ();        Jsonobj.accumulate ("Servers", null);//allow value to be 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.tost         Ring ());        /** * Public jsonobject Element (String key, Object value) */Jsonobject object = new Jsonobject ();        Object.element ("Price", "500");        Object.element ("Price", "1000"); Object: {"Price": "1000"} question: What is the difference between this and put???        Well, it would call accumulate???    System.out.println ("object:" + object.tostring ()); }}

Console Print Results:
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"}

Questions:

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.

Put the key/value pair inside the Jsonobject object. If the current value is NULL, the key will be removed if the key exists. If this key has value before it, then this method calls the accumulate method.

The test is element not true, but the result is not the expected effect as above. Why is it?

Java-net.sf.json's put, accumulate, element practice and questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.