Android reads JSON format data 1. What is JSON?
JSON, all known as JavaScript Object Notation, is intended to be a representation of JavaScript objects.
- JSON is a lightweight text data interchange Format
- JSON independent of language
- JSON has a self-descriptive narrative and is easier to understand
Different than XML:
- No end tag
- Even shorter
- Read and write faster
- Working with arrays
- Do not use reserved words
JSON uses JavaScript syntax to describe descriptive data objects, but JSON is still independent of language and platform. The JSON parser and the JSON library support many different programming languages.
Many other knowledge about JSON content please go to the left:
Http://www.w3school.com.cn/json/index.asp
2. JSON syntax rules
Take the JSON file content used in this article as an example:
Test.json
{ "language":[ {"ID":1, "IDE":"Eclipse", "name":"Java"}, {"ID":2, "IDE":"XCode", "name":"Swift"}, {"ID":3, "IDE":"Visual Studio", "name":"C #"} ], "Cat":"It"}
- Data in name/value pairs
- Data is separated by commas
- Curly braces Save Object
- Square brackets Save Array
The simple combination of the sample we know very well the syntax rules of JSON, each JSON data object is stored in curly braces, so we each JSON data file is in curly braces beginning and ending, the JSON object's data is stored as a key-value pair, the key must be a string, Values can be for example the following various types:
- Number (integer or floating point)
- String (in double-cited)
- Logical value (TRUE or FALSE)
- Array (in square brackets)
- Object (in curly braces)
- Null
In addition, JSON can be nested, JSON objects can be stored in JSON objects, arrays can also hold JSON objects, only to conform to the rules of grammar, all the actual operation needs to be dominated.
For the deep operation of various JSON data, personal advice can be analyzed through the more complex JSON data returned by the developer interface of the major Internet companies, and the Youku developer platform is very good.
Another point to note is that the data is read in JSON format if you want to know the data key and the overall format, otherwise you can simply convert to a string and then through a variety of complex inference (including ":", "{}", "[]" and other delimiter inference) to get the data.
3. Android Read JSON data
Take the action to read and parse the Test.json stored in the assets folder, for example, if it involves a JSON file within the application's local package, or a JSON file inside an SD card, it is actually adding one more step through the constructionFileInputStreamobject to open the corresponding file, and the JSON file under the Assets folder is available via Android's official operating methodgetassets (). Open ("Test.json")Directly get aFileInputStreamObject.
We then constructInputStreamReaderGets the file character stream object, and then constructs theBufferedReaderTo get a buffer stream object that can be read efficiently, the JSON data output becomes a string.
Finally, we can store the JSON data in a string and then use the Android built-inJsonobject, Jsonarrayclass to parse the data.
The various input streams can be tested for:
http://blog.csdn.net/moxie008/article/details/5663488
The following is a detailed Android operating code:
Try{InputStreamReader InputStreamReader =NewInputStreamReader (Getassets (). Open ("Test.json"),"UTF-8"); BufferedReader BufferedReader =NewBufferedReader (InputStreamReader); String Line; StringBuilder StringBuilder =NewStringBuilder (); while(line = Bufferedreader.readline ())! =NULL) {stringbuilder.append (line); } bufferedreader.close (); Inputstreamreader.close (); Jsonobject Jsonobject =NewJsonobject (Stringbuilder.tostring ()); LOG.I ("Testjson","cat="+ jsonobject.getstring ("Cat")); Jsonarray Jsonarray = Jsonobject.getjsonarray ("Language"); for(inti =0; I < jsonarray.length (); i++) {Jsonobject object = Jsonarray.getjsonobject (i); LOG.I ("Testjson","----------------"); LOG.I ("Testjson","Id="+ Object.getint ("id")); LOG.I ("Testjson","Name="+ object.getstring ("Name")); LOG.I ("Testjson","Ide="+ object.getstring ("IDE")); } }Catch(Unsupportedencodingexception e) {E.printstacktrace (); }Catch(IOException e) {E.printstacktrace (); }Catch(Jsonexception e) {E.printstacktrace (); }
The main idea is to obtain the data by constructing the jsonobject object and then using functions such as getString (), GetInt (), Getjsonarray (), and Getjsonobject ( ). Very simple to implement.
Android read JSON format data