In-depth JSON (JSON sample tutorial) __c#

Source: Internet
Author: User
Simple and simple JSON

Author:truly

JSON definition

JSON (JavaScript Object notation) is a lightweight data interchange format that is easy to read and write, and is also easy to machine parse and generate. It is based on a subset of the JavaScript programming language in the ECMA262 Language Specification (version 1999-12, Third edition). JSON has a programming language-independent text format, but it also uses the custom of Class C (including C, C + +, C #, Java, JavaScript, Perl, Python, etc.), which makes JSON an ideal data interchange format.

The structure of JSON is based on the following two points
1. "Name/value" pairs of the collection in different languages, it is understood as objects (object), records (record), structure (struct), Dictionary (dictionary), hash table (hash tables), key list (keyed list), etc. 2. Ordered list of values in most languages, it is understood that array json is used :

JSON represents a JavaScript object in a particular string form. If you assign a string with such a form to any JavaScript variable, the variable becomes an object reference, and the object is constructed from a string, as if it were a bit clumsy, and we use an example to illustrate it.

This assumes that we need to create a user object and have the following properties username User ID username user Email

You can use the following JSON form to represent the user object:

{"UserID": One, "Name": "Truly", "Email": "Zhuleipro hotmail.com"};

Then, if you assign this string to a JavaScript variable, you can use any of the object's properties directly.

Complete code:

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

It might be a bit more complicated to actually use, for example, we define a more detailed structure for name so that it has FirstName and LastName:

{"UserID": One, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "Zhuleipro hotmail.com"}

Complete code:

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

Now that we're adding a new requirement that one of our pages needs a list of users, not just a single user, then you need to create an array of user lists.
The following code demonstrates the use of JSON to define this list of users:

[
{"UserID": One, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "Zhuleipro hotmail.com"},
{"UserID": "Name": {"FirstName": "Jeffrey", "LastName": "Richter"}, "Email": "XXX xxx.com"},
{"UserID": "Name": {"FirstName": "Scott", "LastName": "Gu"}, "Email": "Xxx2 xxx2.com"}
]


Complete code:

<script>
var userlist = [
{"UserID": One, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "Zhuleipro hotmail.com"},
{"UserID": "Name": {"FirstName": "Jeffrey", "LastName": "Richter"}, "Email": "XXX xxx.com"},
{"UserID": "Name": {"FirstName": "Scott", "LastName": "Gu"}, "Email": "Xxx2 xxx2.com"}
];
Alert (userlist[0). Name.firstname);
</script>

In fact, in addition to using the "." Referencing properties, we can also use the following statement:


Now readers should be aware of the use of JSON, summed up in the following points: objects are attributes, a collection of value pairs. The beginning of an object is "{", Ending with "}". Each property name and value is separated by the ":" Prompt, and the attributes are delimited by ",". An array is a collection of sequential values. An array begins with "[", Ends with "]", and the values are separated by ",". Values can be strings in quotes, numbers, true, false, NULL, or an object or an array. These structures can be nested. The definitions of strings and numbers are basically the same as C or Java.

Section :

This article demonstrates a preliminary understanding of the powerful uses of JSON through an example. This can be summed up as follows: JSON provides an excellent object-oriented method for caching metadata to clients. JSON helps isolate the validation data and logic. JSON helps provide the essence of Ajax for WEB applications.

Resources:
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.