Android Create JSON format data
As a supplement to the previous blog post, the simple demo that explains Android's creation of JSON-formatted data.
1. Creating JSON-formatted data
For Android to create JSON-formatted data, the code is very simple, as Android officially provides a method such as put (key, value) , as follows:
Jsonobject Jsonobject =NewJsonobject (); Jsonarray Jsonarray =NewJsonarray (); Jsonobject object_1 =NewJsonobject (); Jsonobject object_2 =NewJsonobject (); Jsonobject Object_3 =NewJsonobject ();Try{Object_1.put ("id",1); Object_1.put ("IDE","Eclipse"); Object_1.put ("Name","Java"); Object_2.put ("id",2); Object_2.put ("IDE","XCode"); Object_2.put ("Name","Swift"); Object_3.put ("id",3); Object_3.put ("IDE","Visual Studio"); Object_3.put ("Name","C #"); Jsonarray.put (object_1); Jsonarray.put (object_2); Jsonarray.put (Object_3); Jsonobject.put ("Languages", Jsonarray); Jsonobject.put ("Cat","It");}Catch(Jsonexception e) {E.printstacktrace ();} LOG.I ("Testjson", jsonobject.tostring ());
The last log log information printed out is:
{"Languages":[{"ID":1,"IDE":"Eclipse","name":"Java"},{"ID":2,"IDE":"XCode","name":"Swift"},{"ID":3,"IDE":"Visual Studio","name":"C #"}],"Cat":"It"}
Android Create JSON format data