The JSON text format is syntactically identical to the code that creates the JavaScript object. Because of this similarity, the JavaScript program can use the built-in eval () function to generate native JavaScript objects with JSON data without a parser.
JSON syntax rules: /c0>
data in name/value pairs data is separated by commas curly Braces Save Object square brackets Save Array
Introduction to JSON name/value pairs
"Name": "Zhang Fei", "Age": 23
the value of JSON can be:
number (integer or floating point) string (to be included in double quotes) logical value (TRUE or false) Array (in square brackets) object (in curly braces)
JSON-to-JavaScript objects are as follows:
Eval ("(" + str + ")");
code example:
<Html><Head><Title>json Test</Title><ScriptSrc= "/scripts/jquery-1.7.2.min.js"Type= "Text/javascript"></Script><ScriptType= "Text/javascript">//Basically, JSON returns either an object or an array, and if you simply return a string, there is no need to use JSON, so//JSON is basically the following, but be aware of double quotesVarStr0="{employees: ' Test '}";VarObj0=Eval"("+Str0+")"); alert (obj0.employees);//Output testVarStr="{name: ' Zhang San ', age:21}";VarObj=Eval"("+Str+")"); document.write (obj.name+Obj. Age);//Output Sheet 321alert (obj.name);//Don't look at the following JSON long, is actually an object, the value of the property employees is an array of objects. Compared to the above, it's just name: Zhang Sanzhong's "Zhang San" string becomes an array of objects.Varstr2=‘{"Employees": [‘+‘{"FirstName": "Bill", "LastName": "Gates"},‘+‘{"FirstName": "George", "LastName": "Bush"},‘+‘{"FirstName": "Thomas", "LastName": "Carter"}]}‘;VarObj2=Eval"("+str2+")"); Alert (obj2.employees[0].firstname);//Pop Up BillVarStr3="[1,2,3,4,5,6]";//JSON arrayVarObj3=Eval"("+Str3+")");//Output 2Alert (obj3[1</script></head><< Span style= "color: #800000;" >body> <div Span style= "color: #ff0000;" >id= "Div1" </< Span style= "color: #800000;" >div></body< Span style= "color: #0000ff;" >></html>
JSON and JavaScript object conversions
JSON to JavaScript Object
json to object 1function jsontoobject (str) { return eval ("(" + str + ")");} //functionvar json = (new function ("return" +return json;} //JSON to object 3 jquery tool function $.parsejson ()
JavaScript Object goto JSON
JavascriptObject Goto JSONfunctionObjecttojson (o) {var arr = []; var FMT = function(s) { if (typeof s = = ' object ' && s! = null) return json2str (s); return/^ (string|number) $/.test (typeof s)? "'" + S + "'" : s;} For (var i in o) arr.push ("'" + i + "':" + FMT (O[i])); return ' {' + arr.join (', ') + '} ';}
JSON syntax, format