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.