Net.sf.json-put, accumulate, element

Source: Internet
Author: User

Net.sf.json the jar required:

Note the version, which conflicts between some versions.

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 ();        Jsonarray Jsonarray = new Jsonarray (); /** * Public Object put (object key, Object value) * Maps value to key * If there is a value before this Jsonobject object in this K        EY, the current value will replace the previous value */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 a value is already present in the         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 printing:

Jsonobject: {"One": "First"}jsonobject: {"One": "First", "One": "Second"}jsonobject: {"One": "First", "One": "Cover"} Jsonobject: {"A": "Cover"}jsonobj: {"Servers": [NULL, "Tomcat"], "Codes": ["Java", "JavaScript"]}object: {"Price": " 1000 "}

P.s. Online posts are described in the element method as follows:

Put a Key/value pair in the jsonobject. If the value is null and then the key would be removed from the jsonobject if it is
Present. If There is a previous value assigned to the key, it'll call accumulate.

Translation:

The public jsonobject element (String key, object value) places the key/value pairs inside the Jsonobject object. If the current value is null (NULL), the key is removed if the key exists. If this
Key is preceded by a value, this method calls the accumulate () method.

However, the pro-test, but not exactly, the results are not as expected on the effect. So I wonder, online about such a lot of posts, are the same, presumably direct CV Dafa, but I am very puzzled, why is not the document that the element?!

Net.sf.json-put, accumulate, element

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.