JsonParse.js 4.2 KB

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