JSON object generated by text. A pair of parentheses must be added outside.
JSON is a subset of JavaScript, so it is very simple to use JSON in JavaScript.
JS Code
- VaR myjsonobject = {"bindings ":[
- {"Ircevent": "PRIVMSG", "method": "newuri", "RegEx": "^ http ://.*"},
- {"Ircevent": "PRIVMSG", "method": "deleteuri", "RegEx": "^ Delete .*"},
- {"Ircevent": "PRIVMSG", "method": "randomuri", "RegEx": "^ random .*"}
- ]
- };
In the above example, we created an object that only contains one member "bindings", and bindings contains an array composed of three objects. All three objects contain three members: "ircevent", "method", and "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
- VaR myobject = 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
- VaR myobject = 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 ){
- Return key. indexof ('date')> = 0? New Date (value): value ;});
The stringifier function is used to convert a JS object into JSON text, opposite to parse.
JS Code
- VaR myjsontext = JSON. stringifier (myobject );