JSON processing with JavaScript

Source: Internet
Author: User
JSON is a subset of JavaScript, so it is very simple to use JSON in JavaScript.

JS Code
  1. VaRMyjsonobject = {"Bindings":[
  2. {< span style =" color: Blue; ">" ircevent " : "PRIVMSG" , "method" : "newuri" , "RegEx" : "^ http ://. * " },
  3. {< span style =" color: Blue; ">" ircevent " : "PRIVMSG" , "method" : "deleteuri" , "RegEx" : "^ Delete. * " },
  4. {< span style =" color: Blue; ">" ircevent " : "PRIVMSG" , "method" : "randomuri" , "RegEx" : "^ random. * " }
  5. ]
  6. };

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

  1. Myjsonobject. bindings [0]. Method

You can use the eval () function to convert a JSON string to an object. JS Code

  1. 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

  1. VaRMyobject = JSON. parse (myjsontext, filter );

The optional filter parameter traverses each value Key Value Pair and performs related processing. For example:

JS Code

  1. Mydata = JSON. parse (text,Function(Key, value ){
    1. 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

  1. VaRMyjsontext = JSON. stringifier (myobject );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.