Jsonobject,jsonarray is the two subclass of JSON.
First we look at jsonobject Source:
You will find that Jsonobject is inherited map<string, Object>, and are all methods used in map. Can say jsonobject equivalent to map<string, object>
Take a look at the concrete examples:
/ ** * Turn map into jsonobject, then add element, output*/@Test Public voidTestjsonobject () {Map<string, object> TestMap =NewHashmap<>(); Testmap.put ("Key1","value1"); Testmap.put ("Key2","value2"); Jsonobject Jsonobj=NewJsonobject (TestMap); Jsonobj.put ("Key3","Value3"); System. out. println (Jsonobj); System. out. println (Jsonobj.Get("Key2")); }
Operation Result:
{"key1":"value1","key2":" value2","key3":"value3" }value2
Look at the source of Jsonarray:
You will find that Jsonarray is an inherited list<object>, and are all methods used in the list. Can say jsonarray equivalent to list<object>
Specific examples:
/ ** * Convert List object to Jsonarray, then output*/@Test Public voidTestjsonarray () {List<Object> list =NewArraylist<>(); List.add ("Home"); List.add ( -); List.add (true); List.add (NewXwjuser (1,"Hello World",NewDate ())); Jsonarray Jsonarr=Jsonarray.parsearray (json.tojsonstring (list)); System. out. println (Jsonarr); }
Operation Result:
["home",max,true, {"ID":1," message":HelloWorld", "sendtime ":1525237337937}]
Fastjson of Jsonobject and Jsonarray