/** * Json:javascript Object Notation (JavaScript object Notation). <br/> * JSON is the syntax for storing and exchanging text information. <br/> * Features:<br/> * JSON is a lightweight text data Interchange Format <br/> * JSON independent of the language and the platform <br/> * json is self-descriptive and more understanding of <br/ the difference between > * and XML:<br/> * XML-like, smaller, faster, easier to parse than XML <br/> * No end tag <br/> * Shorter <br/> * Read and write faster <br/& Gt * Use Arrays <br/> * Do not use reserved words <br/> * * JSON syntax is a subset of JavaScript Object notation syntax. <br/> * Data in name/value pairs <br/> * Data separated by commas <br/> * Curly Braces Save Object <br/> * square brackets Save Array <br/> * * JSON value can be:< br/> * Number (positive or floating point) <br/> * string (in double quotes) <br/> * Logical value (TRUE or FALSE) <br/> * Array (in square brackets) <br/> * Object (in curly braces) & lt;br/> * null<br/> * @author wangzhu * */
1. Reading JSON objects from a file
/*** Read JSON object*/ Private voidReadjsonobject () {String json=ReadFile (); Try{jsonobject root=NewJsonobject (JSON); System.err.println ("Cat=" + root.getstring ("Cat")); Jsonarray Array= Root.getjsonarray ("Language"); for(inti = 0; I < Array.Length (); i++) {System.err.println ("------------"); Jsonobject Object=Array.getjsonobject (i); System.err.println ("Id=" + object.getint ("id"))); System.err.println ("Ide=" + object.getstring ("IDE")); System.err.println ("Name=" + object.getstring ("name"))); } } Catch(jsonexception e) {e.printstacktrace (); } } /*** Read the contents of the file, the text is encoded in UTF-8 * *@return */ PrivateString ReadFile () {StringBuilder Accum=NewStringBuilder (); InputStreamReader ISR=NULL; BufferedReader BR=NULL; Try{ISR=NewInputStreamReader (Getassets (). Open ("Testjson.json"), "UTF-8"); BR=NewBufferedReader (ISR); String Line=NULL; while(line = Br.readline ())! =NULL) {accum.append (line); } } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(BR! =NULL) { Try{br.close (); } Catch(IOException e) {e.printstacktrace (); } } if(ISR! =NULL) { Try{isr.close (); } Catch(IOException e) {e.printstacktrace (); } } } returnaccum.tostring (); }
:
JSON file:
Execution Result:
2. Creating a JSON Object
/*** Create JSON object*/ Private voidCreatejsonobject () {Try{jsonobject root=NewJsonobject (); Root.put ("Cat", "it"); Jsonarray Array=NewJsonarray (); Jsonobject Lan1=NewJsonobject (); Lan1.put ("id", 1); Lan1.put ("IDE", "Eclipse"); Lan1.put ("Name", "Java"); Array.put (LAN1); Jsonobject lan2=NewJsonobject (); Lan2.put ("id", 2); Lan2.put ("IDE", "Xcode"); Lan2.put ("Name", "Swift"); Array.put (LAN2); Jsonobject lan3=NewJsonobject (); Lan3.put ("ID", 3); Lan3.put ("IDE", "Visual Studio"); Lan3.put ("Name", "C #"); Array.put (LAN3); Root.put ("Language", array); System.err.println ("Createjsonobject:" +root.tostring ()); } Catch(jsonexception e) {e.printstacktrace (); } }
Execution Result:
Simple use of JSON data format in Android