JsonParse.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. o2.widget = o2.widget || {};
  2. o2.widget.JsonParse = new Class({
  3. Implements: [Options, Events],
  4. options: {
  5. "topkey": "JSON"
  6. },
  7. initialize: function(json, jsonObjectNode, jsonStringNode, options){
  8. this.setOptions(options);
  9. this.json = json;
  10. this.jsonObjectNode = jsonObjectNode;
  11. this.jsonStringNode = jsonStringNode;
  12. this.stopParseJson = false;
  13. },
  14. load: function(){
  15. this.jsonString = JSON.encode(this.json);
  16. if (this.jsonStringNode) this.jsonStringNode.set("text", JSON.format(this.json));
  17. if (this.jsonObjectNode) this.loadObjectTree();
  18. },
  19. loadObjectTree: function(){
  20. if (this.objectTree){
  21. this.objectTree.node.destroy();
  22. this.objectTree = null;
  23. }
  24. o2.require("o2.widget.Tree", function(){
  25. this.objectTree = new o2.widget.Tree(this.jsonObjectNode, {"style": "jsonview", "text": "text",
  26. "onQueryExpand": function(node){
  27. if (node.firstChild){
  28. if (node.firstChild.options.text==="loadding..."){
  29. node.firstChild.destroy();
  30. var data = node.options.value;
  31. var level = node.options.level+1;
  32. switch (typeOf(data)){
  33. case "object":
  34. for (i in data) this.parseJsonObject(level, node, i, i, data[i], false);
  35. break;
  36. case "array":
  37. data.each(function(v, i){
  38. this.parseJsonObject(level, node, i, i, v, false);
  39. }.bind(this));
  40. break;
  41. }
  42. }
  43. }
  44. }.bind(this)
  45. });
  46. this.objectTree.load();
  47. var jsonStr = JSON.stringify(this.json,null,2);
  48. var str = this.parseJsonObject(0, this.objectTree, "", this.options.topkey || "JSON", this.json, true);
  49. // var jsonStr = str.substring(0, str.length-2);
  50. if (!this.stopParseJson){
  51. if (this.jsonStringNode) this.jsonStringNode.set("text", jsonStr);
  52. }else{
  53. this.stopParseJson = false;
  54. }
  55. }.bind(this));
  56. },
  57. parseJsonObject: function(level, treeNode, title, p, v, expand){
  58. if (this.stopParseJson){
  59. // alert(this.stopParseJson);
  60. return false;
  61. }
  62. var o = {
  63. "expand": expand,
  64. "title": "",
  65. "text": "",
  66. "action": "",
  67. "icon": "",
  68. "value": v,
  69. "level": level
  70. };
  71. var tab = "";
  72. for (var i=0; i<level; i++) tab+=" ";
  73. var title = title;
  74. if (title) title="\""+title+"\": ";
  75. var jsonStr = "";
  76. var nextLevel = level+1;
  77. switch (typeOf(v)){
  78. case "object":
  79. o.text = p;
  80. o.icon = "object.png";
  81. var node = treeNode.appendChild(o);
  82. var jsonStrBegin = tab+title+"{";
  83. var jsonStrEnd = tab+"}";
  84. if (expand){
  85. for (i in v) jsonStr += this.parseJsonObject(nextLevel, node, i, i, v[i], false);
  86. }else{
  87. if (Object.keys(v).length) node.appendChild({ "expand": false, "text": "loadding..."});
  88. }
  89. jsonStr = jsonStrBegin+"\n"+jsonStr.substring(0, jsonStr.length-2)+"\n"+jsonStrEnd+",\n";
  90. break;
  91. case "array":
  92. o.text = p;
  93. o.icon = "array.png";
  94. var node = treeNode.appendChild(o);
  95. var jsonStrBegin = tab+title+"[";
  96. var jsonStrEnd = tab+"]";
  97. if (expand){
  98. v.each(function(item, idx){
  99. jsonStr += this.parseJsonObject(nextLevel, node, "", "["+idx+"]", item, false);
  100. }.bind(this));
  101. }else{
  102. if (v.length) node.appendChild({ "expand": false, "text": "loadding..."});
  103. }
  104. jsonStr = jsonStrBegin+"\n"+jsonStr.substring(0, jsonStr.length-2)+"\n"+jsonStrEnd+",\n";
  105. break;
  106. case "string":
  107. jsonStr += tab+title+"\""+v+"\",\n";
  108. o.text = p + " : \""+v+"\"";
  109. o.icon = "string.png";
  110. var node = treeNode.appendChild(o);
  111. break;
  112. case "date":
  113. jsonStr += tab+title+"\""+v+"\",\n";
  114. o.text = p + " : \""+v+"\"";
  115. o.icon = "string.png";
  116. var node = treeNode.appendChild(o);
  117. break;
  118. case "number":
  119. jsonStr += tab+title+""+v+",\n";
  120. o.text = p + " : "+v+"";
  121. o.icon = "string.png";
  122. var node = treeNode.appendChild(o);
  123. break;
  124. default:
  125. jsonStr += tab+title+v+",\n";
  126. o.text = p + " : "+v;
  127. o.icon = "string.png";
  128. var node = treeNode.appendChild(o);
  129. }
  130. return jsonStr;
  131. }
  132. });