JSON definition and jquery parsing json__js

Source: Internet
Author: User

I. Premises

The full name of JSON is "JavaScript Object notation", which means JavaScript representation, a lightweight data interchange format that is text-based and language-independent. XML is also a data interchange format, so why not choose XML? Because XML can be used as a Cross-platform data interchange format, it is inconvenient to process XML in JS (JavaScript shorthand), while XML tags are more than data, which increases the traffic generated by the exchange, and JSON does not have any additional tags, which can be treated as objects in JS. So we're more inclined to choose JSON to exchange data. JSON (JavaScript Object notation) is a simple data format that is lighter than XML. JSON is a lightweight data interchange format that is ideal for server interaction with JavaScript.

Two. Two structures of JSON

JSON has two representations of structures, objects, and arrays.
The object structure begins with the "{" Brace and ends with the curly braces "}". The middle section consists of 0 or more "key"/value (value) pairs, separated by ":" Between the keyword and the value, and the syntax structure is like code.

{
    key1:value1,
    key2:value2,
    ...
}

Where the keyword is a string, and the value can be a string, numeric, True,false,null, object, or array

The array structure begins with "[", "]" ends. The middle consists of 0 or more list of values separated by "," and the syntax structure is like code.

[
    {
        key1:value1,
        key2:value2 
    },
    {
         key3:value3,
         key4:value4   
    }
]
Three. Understanding the fuzzy concepts of JSON strings and JSON objects

I've never been able to understand the JSON strings and JSON objects and the transitions between them, and then I've written a lot about what people call JSON strings and JSON objects.

String: This is a good explanation of the characters that are included with "double quotes or" single quotes. For example: var comstr = ' This is string ';
JSON string: Refers to a JS string that conforms to the JSON format requirement. For example: var jsonstr = "{studentid: ' m ', Name: ' Tmac ', Hometown: ' USA '}";
JSON object: A JS object that conforms to the JSON format requirements. For example: var jsonobj = {studentid: "M", Name: "Tmac", Hometown: "USA"};

Four. Content

1. JSON definition:

The simple JSON format is [{"Key1": "Value1"},{"Key2": "value2"}],

[] represents the array, {} is the data object in the array, Key1,key2 is a key in a JSON object, a key value in JSON is unique, value1,value2 is the value of the key key.

How to define:

1 directly spell the JSON string, example: String variable, content is [{"Attchname": "Annex 0", "Attchid": 0},{"Attchname": "Annex 1", "Attchid": 1},{"Attchname": "Annex 2", "Attchid": 2}].

2) Introduce Json-lib.jar open source jar package, define Jsonobject object, example: Jsonarray Jsonarray = new Jsonarray (); Jsonobject Attchjson = new Jsonobject (); Attchjson.put ("Attchid", "0");
Attchjson.put ("Attchname", "annex 0"); Jsonarray.put (Attchjson); Jsonarray is a JSON data, which is equivalent to using [] to define a JSON.

Complex JSON definition, the value of the key in JSON can also be a JSON array, for example, in JSON, which encapsulates a task information, there are several attachment definitions in this task: jsonarray taskjsonarray = new Jsonarray (); Jsonobject taskjsonobj = new Jsonobject (); Taskjsonobj.put ("TaskId", 100);
Taskjsonobj.put ("TaskName", "MyTask"); Taskjsonobj.put ("Attchs", Jsonarray); Taskjsonarray.put (Jsonobj);

Taskjsonarray for the last JSON content you want to get,

The form of a simple puzzle is as follows:

[{"Attchs": [{"Attchname": "Attachment 0", "Attchid": 0},{"Attchname": "Annex 1", "Attchid": 1},{"Attchname": "Annex 2", "Attchid": 2}], " TaskId ": M," TaskName ":" MyTask "}]

2. JS parsing json

The general parsing method for JSON:

var json = Eval_r (Jsonarray);
for (Var i=0 i
Alert ("Attchid:" +json[i].attchid+ ", Attchname:" +json[i].attchname);
}












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.