Create a JavaScript string that contains the JSON syntax:
var txt = ' {' Employees ': [' +' {' firstName ': ' John ', ' lastName ': ' Doe '}, ' +' {' firstName ': ' Anna ', "LastName": "Smith"}, ' +' {"FirstName": "Peter", "LastName": "Jones"}]} ';
Because JSON syntax is a subset of JavaScript syntax, theJavaScript function eval () can be used to convert JSON text to JavaScript objects.
The eval () function uses the JavaScript compiler to parse the JSON text and then generate the JavaScript object. you must enclose the text in parentheses in order to avoid syntax errors:
var obj = eval ("(" + txt + ")");
Course:
<H2>Create Object from JSON String</H2><P>First Name:<spanID= "FName"></span><BR>Last Name:<spanID= "LName"></span><BR> </P> <Script>vartxt= '{"Employees": [' +'{"FirstName": "John", "LastName": "Doe"},' +'{"FirstName": "Anna", "LastName": "Smith"},' +'{"FirstName": "Peter", "LastName": "Jones"}]}';varobj=Eval ("(" +txt+ ")");//document.getelementbyid ("fname"). InnerHTML=obj.employees[1].firstname document.getElementById ("lname"). InnerHTML=obj.employees[1].lastname</Script>
Json.parse (text, reviver)
text required . A valid JSON string.
Reviver
Optional . A function that transforms the result. This function is called for each member of the object. If the member contains nested objects, the nested objects are converted before the parent object. For each member, the following conditions occur:
If Reviver returns a valid value, the member value is replaced with the converted value.
If Reviver returns the same value it receives, the member value is not modified.
If reviver returns null or undefined, the member is deleted.
Parse is used to parse a JSON object from a string, [using Json.parse to convert a JSON string to an object]
Such as
cankao:http://blog.csdn.net/lowkeysk/article/details/8175195
var str = ' {' name ': ' Huangxiaojian ', ' age ': ' 23 '} '
Results:
Json.parse (str)
Object
- Age: "All"
- Name: "Huangxiaojian"
- __proto__: Object
Note: Single quotes are written in {}, each property name must be in double quotation marks, or an exception will be thrown.
Stringify () is used to parse out a string from an object, such as
var a = {A:1,b:2}
Results:
Json.stringify (a)
"{" A ": 1," B ": 2}"
<H2>Create Object from JSON String</H2><P>First Name:<spanID= "FName"></span><BR>Last Name:<spanID= "LName"></span><BR> </P> <Script>vartxt= '{"Employees": [' +'{"FirstName": "John", "LastName": "Doe"},' +'{"FirstName": "Anna", "LastName": "Smith"},' +'{"FirstName": "Peter", "LastName": "Jones"}]}'; obj=json.parse (TXT);//document.getelementbyid ("fname"). InnerHTML=obj.employees[1].firstname document.getElementById ("lname"). InnerHTML=obj.employees[1].lastname</Script>
JSONP Knowledge [JSON withPadding]: is a "usage mode" of JSON that allows Web pages to retrieve data from other domain names (sites), that is, cross-domain reading
Why do we need a special technology (JSONP) to access data from different domains (websites)? This is because of the homologous policy .
The same-origin policy, which is a well-known security policy presented by Netscape, is now used by all JavaScript-enabled browsers.
In JS, the JSON string is parsed into a JSON data format, typically in two ways:
1. One for using the eval () function.
2. Use the function object for return parsing.
Use
Use the Eval function to parse, and use the each method of jquery to traverse
The result of a jquery request is a JSON object
Reference: http://www.cnblogs.com/myjavawork/archive/2011/03/10/1979279.html
Http://www.educity.cn/wenda/195035.html
After the server returns a JSON string, how can it not be called in jquery? How to convert to object?
The string returned by the server is: {"Address": "China", "name": "ABC"}
I use jquery's post:
$.post ("Http://localhost:8080/testjquery/ok/testaction", function (data) {
alert (data); Popup: {"address":"china","name":"abc"}
alert (data.address); No show, pop undefine
}, "text");
}
Is how the client receives the JSON string returned by the server and converts it to a JS JSON object, which accesses the data in the same way as the object.
First Use JS's eval function to convert data to an object
The data you return is not JSON data at all, there is a way to return JSON type data in jquery, or you can specify the return type as JSON type when $.post
About Data parsing