Net.sf.json
net.sf.json
Need 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