o2.widget = o2.widget || {}; o2.widget.JsonParse = new Class({ Implements: [Options, Events], options: { "topkey": "JSON" }, initialize: function(json, jsonObjectNode, jsonStringNode, options){ this.setOptions(options); this.json = json; this.jsonObjectNode = jsonObjectNode; this.jsonStringNode = jsonStringNode; this.stopParseJson = false; }, load: function(){ this.jsonString = JSON.encode(this.json); if (this.jsonStringNode) this.jsonStringNode.set("text", JSON.format(this.json)); if (this.jsonObjectNode) this.loadObjectTree(); }, loadObjectTree: function(){ if (this.objectTree){ this.objectTree.node.destroy(); this.objectTree = null; } o2.require("o2.widget.Tree", function(){ this.objectTree = new o2.widget.Tree(this.jsonObjectNode, {"style": "jsonview", "text": "text", "onQueryExpand": function(node){ if (node.firstChild){ if (node.firstChild.options.text==="loadding..."){ node.firstChild.destroy(); var data = node.options.value; var level = node.options.level+1; switch (typeOf(data)){ case "object": for (i in data) this.parseJsonObject(level, node, i, i, data[i], false); break; case "array": data.each(function(v, i){ this.parseJsonObject(level, node, i, i, v, false); }.bind(this)); break; } } } }.bind(this) }); this.objectTree.load(); var jsonStr = JSON.stringify(this.json,null,2); var str = this.parseJsonObject(0, this.objectTree, "", this.options.topkey || "JSON", this.json, true); // var jsonStr = str.substring(0, str.length-2); if (!this.stopParseJson){ if (this.jsonStringNode) this.jsonStringNode.set("text", jsonStr); }else{ this.stopParseJson = false; } }.bind(this)); }, parseJsonObject: function(level, treeNode, title, p, v, expand){ if (this.stopParseJson){ // alert(this.stopParseJson); return false; } var o = { "expand": expand, "title": "", "text": "", "action": "", "icon": "", "value": v, "level": level }; var tab = ""; for (var i=0; i