Step through the JSON

Source: Internet
Author: User

What is JSON

JSON is an abbreviation for JavaScript object Notation (JavaScript objects notation), which is a lightweight data format and is based on a subset of JavaScript. As with XML, JSON is also a syntax for storing and exchanging textual information. JSON is smaller, faster, and easier to parse than XML. . These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate.

JSON Grammar Rules

Explicit JSON syntax is a subset of JavaScript Object notation syntax:

    • value in Name / value pairs in
    • Data is separated by commas
    • Curly braces Hold objects
    • Square brackets hold an array

The format for writing JSON data is a name/value pair. The exact format is name: value, name, and value pairs can all be field names (including in double quotes), as follows:

"Name": "Binghuojxj"

The above statement is equivalent to the following JavaScript statement:

Name= "BINGHUOJXJ";

The value of JSON can be:

    • Number (integer or floating point)
    • Object (in curly braces)
    • String (in double quotes)
    • Array (in square brackets)
    • Boolean value (True or FALSE)
    • Null
JSON Infrastructure

There are two constructs in JSON, JSON representing the objects and arrays of JavaScript, so these two structures are objects and arrays. With objects and arrays, JSON can represent a variety of complex structures.

Object: The object in JS is represented as "{}" in the content, the data structure is {Key:value,key:value,...}, as follows:

{

"FirstName": "George",

"LastName": "Bush"

}

In an object-oriented language, key is the property of an object, through an object. Key can access the value of the corresponding property, which can be a number, a string, an object, an array of several.

Array: The contents of the array represented in JS as "[]", the data structure is as follows:

["Java", "JavaScript", "VB",...]

The values are obtained by index, as in all languages, and the values within the array can be numbers, strings, objects, arrays, and so on.

JSON Base Example

Examples of objects:

Examples of arrays:

Object Array Nesting Example:

JSON and XML instance Comparisons

The XML and JSON files are as follows:

. Net Operational JSON data

  1. you follow syntax format, manual stitching json
  2. through Open source class library < Span style= "Font-family:verdana; Background-color: #faf7ef; " >newtonsoft.json jobject , jsonreader and jsonwriter to operate, this is really arbitrary, the masses of the optometrist discerning ha.

JsonTextReader Read

  

Operation Result:

Jobject Read

Operation Result:

To create a JSON string using Jsonwriter

The results of the operation are as follows:

  

Use Jsonserializer to read and write JSON objects (based on jsonreader and jsonwriter)

  

Operation Result:

  

Where the person class is defined as follows:

The entire code looks like this:

//Jsonreader Read JSON string            Const stringJsontext =@"{"FirstName" ":" " Brett" "," "LastName" ":" "McLaughlin" "}"; varReader =NewJsonTextReader (NewStringReader (Jsontext));  while(reader. Read ()) {Console.WriteLine (reader. Tokentype+"\t\t"+ Reader. ValueType +"\t\t"+Reader.            Value); }            //using JOBEJCT to read JSON strings            Const stringJsontext =@"{"FirstName" ":" " Brett" "," "LastName" ":" "McLaughlin" "}"; varJobect =Jobject.parse (Jsontext); varValues = Jobect.properties (). Select (item =item. Value.tostring ()).            ToArray (); foreach(varValueinchvalues) {Console.Write (Value+" "); }            //To create a JSON string using Jsonwriter            varStringWriter =NewStringWriter (); varJsonwriter =NewJsonTextWriter (StringWriter);            Jsonwriter.writestartobject (); Jsonwriter.writepropertyname ("FirstName"); Jsonwriter.writevalue ("Brett"); Jsonwriter.writepropertyname ("LastName"); Jsonwriter.writevalue ("McLaughlin");            Jsonwriter.writeendobject ();            Jsonwriter.flush (); varJsontext =Stringwriter.getstringbuilder ().            ToString (); Console.WriteLine ("The resulting JSON string is:"+jsontext); //use Jsonserializer to read and write objects (based on Jsonwriter and Jsonreader)            varperson =NewPerson () {FirstName ="Xiaojun", LastName ="Ju"}; varSerializer =NewJsonserializer (); varStringWriter =NewStringWriter (); Serializer.            Serialize (Stringwriter,person); Console.WriteLine ("the built JSON string is:"+Stringwriter.getstringbuilder ().            ToString ()); varStringReader =NewStringReader (@"{"FirstName" ":" " Xiaojun" "," "LastName" ":" "Ju" "}"); varPerson1 = (person) serializer. Deserialize (NewJsonTextReader (StringReader),typeof(person)); Console.WriteLine (Person1. LastName+"  "+Person1.            FirstName); Console.readkey ();
View Code

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.