Simple use of JSON data format in Android

Source: Internet
Author: User

/** * 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

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.