Introduction to JSON
- JavaScript objects notation (JavaScript object Notation)
- is the syntax for storing and exchanging textual information
- Characteristics
- is a lightweight text data interchange Format
- Independent of language and platform
- Self-descriptive and easier to understand
JSON and XML
- XML-like, smaller, faster, and easier to parse than XML
- No end tag
- Even shorter
- Read and write faster
- Working with arrays
- Do not use reserved words
JSON syntax
- is a subset of the JavaScript Object notation syntax
- Array in key-value pairs
- Data is separated by commas
- Curly braces Save Object
- Save array in brackets
- The JSON value can be
- Number (integer or floating point)
- String (in double quotes)
- Logical value (TRUE or FALSE)
- Array
- Object
- Null
JSON object
-
Written in curly braces, you can include multiple key-value pairs, such as
{"FirstName": "John", "LastName": "Doe"}
JSON array
-
Written in brackets, you can include multiple objects, separated by commas
{ "Employees": [ {"firstName": "John", "LastName": "Doe"}, {"firstName": " Anna "," LastName ":" Smith "}, {" firstName ":" Peter "," LastName ":" Jones "} ]}
Reading JSON data in Android
-
Preparing Data Test.json
{ "languages": [ {"id": 1, "IDE": "Eclipse", "name": "Java"}, {"id" : 2, "IDE": "XCode", "name": "Swift"}, {"id": 3, "IDE": "Visual Sutdio", "name": "C #"} ], "Cat": "It"}
- Copy the Test.json to the project's assets directory
-
Reads the contents of Test.json into a string
StringBuilder Sbuilder =NewStringBuilder ();Try{InputStreamReader ISR=NewInputStreamReader (Getassets (). Open ("Test.json"), "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); String Line; while(line = Br.readline ())! =NULL) {sbuilder.append (line); } br.close (); Isr.close ();} Catch(IOException e) {//TODO auto-generated Catch blocke.printstacktrace ();}
-
Parsing and displaying JSON data
Try{jsonobject root=NewJsonobject (sbuilder.tostring ()); LOG.D ("Tag", "cat=" + root.getstring ("Cat")); Jsonarray Array= Root.getjsonarray ("Languages"); for(inti = 0; I < Array.Length (); i++) {Jsonobject LAN=Array.getjsonobject (i); LOG.D ("Tag", "---------------------------------"); LOG.D ("Tag", "id=" + lan.getint ("id"));//Note that the Get method is consistent with the actual data type in the JSONLOG.D ("Tag", "ide=" + lan.getstring ("IDE"))); LOG.D ("Tag", "name=" + lan.getstring ("name"))); }} Catch(jsonexception e) {e.printstacktrace ();}
- View results in Logcat
Create JSON-formatted data in Android
- Requirements: Create and Test.json exactly the same data in the previous section
-
The code is as follows
Try{jsonobject root=NewJsonobject (); //1. Create a cat attribute and assign a valueRoot.put ("Cat", "it"); //2. Creating an object in an array//{"id": 1, "IDE": "Eclipse", "name": "Java"}Jsonobject Lan1 =NewJsonobject (); Lan1.put ("id", 1); Lan1.put ("IDE", "Eclipse"); Lan1.put ("Name", "Java"); //{"id": 2, "IDE": "XCode", "name": "Swift"}Jsonobject lan2 =NewJsonobject (); Lan2.put ("id", 2); Lan2.put ("IDE", "XCode"); Lan2.put ("Name", "Swift"); //{"id": 3, "IDE": "Visual Sutdio", "name": "C #"}Jsonobject lan3 =NewJsonobject (); Lan3.put ("ID", 3); Lan3.put ("IDE", "Visual Sutdio"); Lan3.put ("Name", "C #"); //3. Placing an object in an arrayJsonarray array =NewJsonarray (); Array.put (LAN1); Array.put (LAN2); Array.put (LAN3); //add an array to rootRoot.put ("Languages", array); LOG.D ("Tag", root.tostring ());} Catch(jsonexception e) {e.printstacktrace ();}
- View results in Logcat
Using the self-contained API to manipulate JSON in Android