In-depth introduction to JSON

Source: Internet
Author: User

Author: Truly

JSON Definition

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write, and easy to parse and generate by machines. It is based on a subset of the JavaScript programming language in the ECMA262 language specification (-12. JSON uses a text format unrelated to the programming language, but also uses C-like languages (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on, these features make JSON an ideal data exchange format.

The JSON structure is based on the following two points:

  • 1. Set of "name/value" PairsIn different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list
  • 2. ordered list of valuesMost languages are understood as arrays)

JSON usage:

JSON represents a JavaScript Object in a specific string. If a string with such a form is assigned to any JavaScript variable, the variable becomes an object reference, and the object is constructed by the string. It seems like a bit of an interface, we still use instances to describe.

Assume that we need to create a User object with the following attributes

  • User ID
  • User Name
  • User Email

    You can use the following JSON format to represent a User object:

    {"UserID":11, "Name":"Truly", "Email":"zhuleipro◎hotmail.com"};

    If you assign this string to a JavaScript variable, you can directly use any attribute of the object.

    Complete code:

    <script>
    var User = {"UserID":11, "Name":"Truly", "Email":"zhuleipro◎hotmail.com"};alert(User.Name);</script>

    In actual use, it may be a little more complicated. For example, we define a more detailed structure for Name to make it have FirstName and LastName:

    {"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"}

    Complete code:

    <script>
    var User = {"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"};alert(User.Name.FirstName);</script>

    Now we want to add a new requirement. A page requires a user list, not just a single user information. here we need to create an array of user lists.
    The following code defines the user list in JSON format:

    [{"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"},{"UserID":12, "Name":{"FirstName":"Jeffrey","LastName":"Richter"}, "Email":"xxx◎xxx.com"},{"UserID":13, "Name":{"FirstName":"Scott","LastName":"Gu"}, "Email":"xxx2◎xxx2.com"}]

    Complete code:

    <script>var UserList = [{"UserID":11, "Name":{"FirstName":"Truly","LastName":"Zhu"}, "Email":"zhuleipro◎hotmail.com"},{"UserID":12, "Name":{"FirstName":"Jeffrey","LastName":"Richter"}, "Email":"xxx◎xxx.com"},{"UserID":13, "Name":{"FirstName":"Scott","LastName":"Gu"}, "Email":"xxx2◎xxx2.com"}];alert(UserList[0].Name.FirstName);</script>

    In fact, in addition to using "." To reference attributes, we can also use the following statement:

    Alert (UserList [0] ["Name"] ["FirstName"]); or alert (UserList [0]. Name ["FirstName"]);

    Now, the reader should have some knowledge about the use of JSON, which is summarized as follows:

  • An object is a set of attributes and value pairs. An object starts with "{" and ends with "}". Use the ":" prompt for each attribute name and value, and separate the attributes.
  • An array is a set of ordered values. An array starts with "[", ends with "]", and values are separated.
  • The value can be a string, number, true, false, or null in quotation marks, or an object or array. These structures can be nested.
  • The definition of strings and numbers is basically the same as that of C or Java.

    Section:

    This article demonstrates how to use JSON through an example. It can be summarized as follows:

  • JSON provides an excellent object-oriented method to cache metadata on the client.
  • JSON helps to separate verification data and logic.
  • JSON helps provide the essence of Ajax for Web applications.

    References:
    Http://www.json.org/

  • 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.