Using the self-contained API to manipulate JSON in Android

Source: Internet
Author: User

Introduction to JSON
    • JavaScript objects notation (JavaScript object Notation)
    • is the syntax for storing and exchanging textual information
    • Characteristics
      1. is a lightweight text data interchange Format
      2. Independent of language and platform
      3. 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
  1. 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"}
  2. Copy the Test.json to the project's assets directory
  3. 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 ();}
  4. 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 ();}
  5. View results in Logcat

    

Create JSON-formatted data in Android
  1. Requirements: Create and Test.json exactly the same data in the previous section
  2. 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 ();}
  3. View results in Logcat

    

Using the self-contained API to manipulate JSON 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.