JSON is a subset of JavaScript, so it is very simple to use JSON in JavaScript.
JS Code
-
- VaRMyjsonobject = {"Bindings":[
- {< span style =" color: Blue; ">" ircevent " : "PRIVMSG" , "method" : "newuri" , "RegEx" : "^ http ://. * " },
- {< span style =" color: Blue; ">" ircevent " : "PRIVMSG" , "method" : "deleteuri" , "RegEx" : "^ Delete. * " },
- {< span style =" color: Blue; ">" ircevent " : "PRIVMSG" , "method" : "randomuri" , "RegEx" : "^ random. * " }
- ]
-
- };
In the preceding example, we created a project that contains only one member."Bindings"An object,Bindings contains an array composed of three objects. All three objects contain three members:"Ircevent", "method", "RegEx ".
In JavaScript, the member can be obtained through the "point.
For example, JS Code
-
- Myjsonobject. bindings [0]. Method
You can use the eval () function to convert a JSON string to an object. JS Code
- VaRMyobject = eval ('+ myjsontext + ')');
The eval function is very fast, but it can compile any external cirpt code, which may cause security issues. The use of Eval is based on the assumption that input code parameters are reliable. In some cases, the client may be untrusted.
For security reasons, it is best to use a JSON parser. A json parser will only accept JSON text. Therefore, it is safer. JS Code
- VaRMyobject = JSON. parse (myjsontext, filter );
The optional filter parameter traverses each value Key Value Pair and performs related processing. For example:
JS Code
- Mydata = JSON. parse (text,Function(Key, value ){
- ReturnKey. indexof ('date')> = 0?NewDate (value): value ;});
The stringifier function is used to convert a JS object into JSON text, opposite to parse.
JS Code
- VaRMyjsontext = JSON. stringifier (myobject );