jquery processing JSON and Ajax return JSON instance

Source: Internet
Author: User
Tags eval json

First, some basic knowledge of JSON.

The objects in JSON are identified by "{}", and a "{}" represents an object, such as {"Areaid": "123"}, and the object's value is the form of a key-value pair (key:value).

"[]", identifies an array, and splits the data within the array through "," such as ["Areaid": "123", "Areaid": "345"].

In many cases it's an array of objects, and that's it:

The code is as follows Copy Code

[{"Areaid": "123"},{"Areaid": "345"}]

In fact, an array is also an object, and the format above can be written like this:

The code is as follows Copy Code

{' area ': [{' Areaid ': ' 123 '},{' areaid ': ' 345 '}]}

This represents an area object, which has two child data, each of which is an object, and each child object is areaid.

The definition format for strings and characters in JSON is similar to the general Class C language definition, with double quotes defining strings and single quotes defining characters.

The key in JSON is enclosed in double quotes, such as "area" and "Areaid", which are enclosed in double quotes, and you can use escape character escape double quotes when constructing JSON strings in some languages.

Two, javascript operation JSON characters

1, first to distinguish between JSON strings and JSON objects

JSON string:

The code is as follows Copy Code

Var Strjson = ' {' area ': [{' Areaid ': ' 123 '},{' areaid ': ' 345 '}]} '

It can also be written in this way:

The code is as follows Copy Code

Var Strjson = ' {' area ': [{' Areaid ': ' 123 '},{' areaid ': ' 345 '}]} ',

This represents a JSON string, because single and double quotes in JS can represent a string, so the first one to use double quotes and the second to use single quotes represents a JSON string.

Here's a look at the JSON object

The code is as follows Copy Code

Var JSON = {"Area": [{"Areaid": "123"},{"Areaid": "345"}]},

See, the JSON object has no single or double quotes on the outside, which means a JSON object.


Script broken in server:

The code is as follows Copy Code

<?php
$data [' id '] = 1;
$dat [' name '] = ' Mary ';
$da [' Red ']= array_merge ($data, $dat);
$data 1[' id '] = 2;
$dat 1[' name '] = "swallow";
$da [' Blue ']= array_merge ($data 1, $dat 1);
Print_r ($DA);///printed out is a two-dimensional array (as follows)

/*
Array
(
[Red] => Array
(
[ID] => 1
[Name] => Mary
)
[Blue] => Array
(
[ID] => 2
[Name] => Swallow
)
)
*/

echo Json_encode ($DA);//output is a string converted to JSON format, which can be used directly in JS (as follows)
/*
{"Red": {"id": 1, "name": "Mary"}, "Blue": {"id": 2, "name": "U71d5u5b50"}}
*/
?>

jquery script:

Return to the processing after JS:
The first to use Varl conversion: When the string is the time to use eval into the jquery object (below)

The code is as follows Copy Code
var arr = ' {' red ': {' id ': 1, ' name ': ' Mary '}, ' Blue ': {' id ': 2, ' name ': ' U71d5u5b50 '} ';//u71d5u5b50 this is automatically converted in PHP
var dataobj = eval ("(" +arr+) ")//Here's the reason to add braces and double quotes. I don't know, just as JSON syntax, you can only memorize
$.each (Dataobj,function (Idx,item) {
Output
Alert (item.id+ "haha" +item.name);
})

The second: No need to transform:

The code is as follows Copy Code
var arr = {"Red": {"id": 1, "name": "Mary"}, "Blue": {"id": 2, "name": "U71d5u5b50"};
$.each (Arr,function (Idx,item) {
Output
Alert (item.id+ "haha" +item.name);
})

There are two ways of looping:
Method One:

The code is as follows Copy Code
$.each (Arr,function (Idx,item) {
Output
Alert (item.id+ "haha" +item.name);
})

Method Two:

The code is as follows Copy Code
for (var key in arr) {
alert (key);
alert (arr[key].status);
}

We can try the effect.

How Ajax is processed when it returns JSON

1, using the normal ASPX page to handle
I think this is the easiest way to deal with it, look at the code below

The code is as follows Copy Code
$.ajax ({
Type: "Post",
URL: "Default.aspx",
DataType: "JSON",
Success:function (data) {
$ ("Input#showtime"). Val (Data[0].demodata);
},
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
alert (Errorthrown);
}
});


This is the code that passes the data in the background

  code is as follows copy code
response.clear ();
                         Response.Write ("[{" Demodata ":" This is the JSON Data "}]");
                         Response.Flush ();
                         Response.End ();

         this way of handling data that is passed directly into JSON data, In other words, the front desk JS code may directly parse the data into JSON object data, rather than string data, such as Data[0].demodata, where this JSON object data is used directly

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.