Json 1 in android

Source: Internet
Author: User

Json 1 in android

JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy for reading and writing, and easy for machine parsing and generation (network transmission speed ).

 

JSON syntax

 

JSON syntax rules JSON syntax is a subset of the JavaScript Object Representation syntax.
  • Data in name/value pairs
  • Data is separated by commas (,).
  • Brackets save objects
  • The format of JSON data written by square brackets to save the JSON name/value pair is name/value pair. Name/value pair combination names are written in front (in double quotation marks), value pairs are written in the back (also in double quotation marks), separated by colons:
    1 "firstName":"John"
    This is easy to understand. It is equivalent to this JavaScript statement:
    1 firstName="John"
    The JSON value can be:
    • Number (integer or floating point number)
    • String (in double quotation marks)
    • Logical value (true or false)
    • Array (in square brackets)
    • Object (in curly brackets)
    • Null

    • Basic example

       

       

      Name/value pair

       

       

      1 {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}
      Array

       

      If JSON is used, you only need to group multiple records with curly braces:
      1 2 3 4 5 6 7 { "people":[ {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}, {"firstName":"Jason","lastName":"Hunter","email":"bbbb"}, {"firstName":"Elliotte","lastName":"Harold","email":"cccc"} ] }
      1 2 3 4 5 6 7 8 9 10 11 12 13 14 {"programmers":[ {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}, {"firstName":"Jason","lastName":"Hunter","email":"bbbb"}, {"firstName":"Elliotte","lastName":"Harold","email":"cccc"} ], "authors":[ {"firstName":"Isaac","lastName":"Asimov","genre":"sciencefiction"}, {"firstName":"Tad","lastName":"Williams","genre":"fantasy"}, {"firstName":"Frank","lastName":"Peretti","genre":"christianfiction"} ], "musicians":[ {"firstName":"Eric","lastName":"Clapton","instrument":"guitar"}, {"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"} ]}

       

       

      Assign a value to a variable. For example, you can create a new JavaScript variable and assign a value to the JSON-format data string: [3]
      1 2 3 4 5 6 7 8 9 10 11 12 13 var people={"programmers":[{"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"}, {"firstName":"Jason","lastName":"Hunter","email":"bbbb"}, {"firstName":"Elliotte","lastName":"Harold","email":"cccc"} ], "authors":[ {"firstName":"Isaac","lastName":"Asimov","genre":"sciencefiction"}, {"firstName":"Tad","lastName":"Williams","genre":"fantasy"}, {"firstName":"Frank","lastName":"Peretti","genre":"christianfiction"} ], "musicians":[ {"firstName":"Eric","lastName":"Clapton","instrument":"guitar"}, {"firstName":"Sergei","lastName":"Rachmaninoff","instrument":"piano"} ]}
      Although the access data does not seem obvious, the long string above is actually an array. After you put this array into the JavaScript variable, you can easily access it. In fact, you only need to use the dot notation to represent array elements. Therefore, to access the first project name in the programmers list, you only need to use the following code in JavaScript:
      1 people.programmers[0].lastName;
      Note that the array index starts from scratch. Therefore, this line of code first accesses the data in the people variable, then moves to the entry called programmers, and then to the first record ([0]). Finally, it accesses the value of the lastName key. The result is the string value "McLaughlin ". The following are examples of using the same variable.
      1 2 3 people.authors[1].genre//Valueis"fantasy" people.musicians[3].lastName//Undefined.Thisreferstothefourthentry,andthereisn'tone people.programmers[2].firstName//Valueis"Elliotte"
      With this syntax, You can process data in any JSON format without using any additional JavaScript toolkit or API. To modify data, you can use periods and square brackets to access the data. You can also easily modify the data in the same way:
      1 people.musicians[1].lastName="Rachmaninov";
      After converting a string to a JavaScript Object, you can modify the data in the variable as follows. The final conclusion of returning the string is that if you want to process a large number of JavaScript objects, JSON is a good choice, so that you can easily convert the data to a format that can be sent to the server program in the request.

       

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.