JSON (JavaScript object Notation) JS objects symbol
is a lightweight format for data interchange
JavaScript eval () function implementation
(i) standard format
function jsonformatting () { var jsonstring = '{' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' age ':' ""} '; var jsonobject = eval ('('jsonstring') '); var tt = '; $.each (Jsonobject,function (k, v) { tt + = k + ":" + v+ "<br/>"; }); $ ("#divmessage"). HTML (TT); }
The notation is that the key value is surrounded by double quotes. This format can be called a json string. converts a JSON string to a JSON object through the Eval function .
(ii) abbreviated format
function jsonformatting () { var jsonstring = '{Unid: ' 1 ', CustomerName: ' Song Jiang ', Age:' "} '; var jsonobject = eval ('('jsonstring') '); var tt = '; $.each (Jsonobject,function (k, v) { tt + = k + ":" + v+ "<br/>"; }); $ ("#divmessage"). HTML (TT); }
The key value omits the quotation marks and converts the JSON string into a JSON object through the Eval function, which can then be accessed in two ways:
@1. Dot Syntax object.property
var jsonstring = ' {' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' Age ': ' ""} '; var jsonobject = eval (' (' + jsonstring + ') '); Alert ( Jsonobject.customername);
@2. Index of key-value pairs
Object[key]
var jsonstring = ' {' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' Age ': ' ""} '; var jsonobject = eval (' (' + jsonstring + ') '); Alert ( jsonobject["CustomerName"]);
eval function Description:
It can execute a string as if it were a JavaScript expression.
The Eval function receives a parameter s, and if s is not a string, it returns s directly. Otherwise, the S statement is executed.
If the S statement execution result is a value, this value is returned, otherwise undefined is returned. Object declaration syntax '{} ' does not return a value , it needs to be enclosed in parentheses to return a value.
function Evaltest () { var code1 = ' A ' + 2 '; Expression var Code2 = ' {a:2} '; Statement alert (eval (code1)); Output A2 alert (eval (code2)); Output undefined alert (eval (' (' + Code2 + ') ');//Output [Object Object]}
For an object declaration statement, it is only execution and cannot return a value.
In order to return an object declaration statement such as a commonly used "{}", it must be enclosed in parentheses to convert it to an expression in order to return its value.
Adds a conversion method from JSON strings to JSON objects for jquery.
Open the library and locate Jquery.extend ({
Add method
Jsontoobject:function (a) { return eval (' (' + A + ') ');
Use the following:
var jsonstring = ' {' Unid ': ' 1 ', ' CustomerName ': ' Song Jiang ', ' Age ': ' ""} '; var jsonobject = $.jsontoobject (jsonstring); alert ( jsonobject["CustomerName"]);
Where jsonstring is a json string, and Jsonobject is a JSON object
jquery implementation
$.parsejson ("Jsonstr"); Convert a JSON string to a JSON object
Using the Json.js Package plugin
var obj = json.parse (Strjson);
JSON object to JSON string
var str = json.stringify (obj) needs to import Json.js package
function jsonformatting () { var jsonobject = {"Unid": "1", "CustomerName": "Song Jiang", "Age": "$"}; var jsstr=json.stringify (jsonobject); Alert (typeof jsstr); }
JSON string-to-JSON object conversion