Android read JSON format data

Source: Internet
Author: User

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 is self-descriptive and 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 data objects, but JSON is still independent of language and platform. The JSON parser and the JSON library support many different programming languages.

For more information on 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

As a simple example, we know the syntax of JSON very well, and every JSON data object is stored in curly braces, so each of our JSON data files begins and ends with curly braces; The JSON object's data is stored as a key-value pair, and the key must be a string. The values can be of the following types:

    • Number (integer or floating point)
    • String (in double quotes)
    • 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, as long as the syntax rules, everything to the actual operational needs of the main.
For the deep operation of various JSON data, personal advice can be analyzed by the more complex JSON data returned by the developer interface of major Internet companies, and the Youku developer platform is very good.
It is also important to note that the data is read in JSON format if you want to know the data key and the overall format, otherwise you may only be converted to a string and then through a variety of complex judgments (including ":", "{}", "[]" and other delimiters to determine the data).

3. Android Read JSON data

Take the action to read and parse the Test.json stored in the assets directory, for example, if it involves a JSON file within the application's local package, or a JSON file inside the SD card, it is actually a step further through the constructionFileInputStreamobject to open the appropriate file, and the JSON file in the assets directory can be provided 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, it is possible to say that 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.
For different input streams, refer to:
http://blog.csdn.net/moxie008/article/details/5663488

Here is the specific 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.