Collect several JSON-related program code segments

Source: Internet
Author: User
A json data string, and then use the eval () function in JavaScript scripts.

A json data-type token string. Then, you can directly access the JSON object by using the eval () function in the JavaScript script. It is more convenient and flexible. Note that a pair of parentheses must be added at both ends of the returned string:

var json1 = web_TreeView_TreeDemo.GetJson().value;json1 = eval('(' + json1 + ')');  
function showJSON()      {          var user =               {           "username":"andy",          "age":20,          "info": { "tel": "123456", "cellphone": "98765"},          "address":                  [                   {"city":"beijing","postcode":"222333"},                         {"city":"newyork","postcode":"555666"}                   ]               }               alert(user.username);               alert(user.age);               alert(user.info.cellphone);               alert(user.address[0].city);               alert(user.address[0].postcode);             user.username = "Tom";               alert(user.username);          }    

Convert a string to a json object:

// Use the eval function to convert the string to the json object function myEval () {var str = '{"name": "viotlet", "occupation": "character "}'; var obj = eval ('+ str +'); alert (obj. toJSONString ());}
// Use json. function myeval () {var str = '{"name": "Violet", "occupation": "character"}'; var obj = str. parseJSON (); alert (obj. toJSONString ());}

Convert a json object to a string:

function showCar() {           var carr = new Car("Dodge", "Coronet R/T", 1968, "yellow");           alert(carr.toJSONString());       }             function Car(make, model, year, color)       {            this.make  =  make;            this.model  =  model;            this.year  =  year;            this.color  =  color;       }  
Function test () {var myJSONtext = {"bindings": [{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^ http: //. * "},{" ircEvent ":" PRIVMSG "," method ":" deleteURI "," regex ":" ^ delete. * "},{" ircEvent ":" PRIVMSG "," method ":" randomURI "," regex ":" ^ random. * "}]}; var myObject = eval (myJSONtext); alert (" Object length: "+ myObject. bindings. length); for (var I = 0; I
 
  

This article is available at http://www.nowamagic.net/librarys/veda/detail/1248.

Related Article

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.