Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | try{ |
| 2 | var jsonStr='{}'; |
| 3 | jsonObj=JSON.parse(jsonStr); |
| 4 | var htmlObj ={}; |
| 5 | var objectId =0; |
| 6 | var level=0; |
| 7 | var htmlStr="" |
| 8 | htmlStr += "<html>"; |
| 9 | htmlStr += "<head>"; |
| 10 | htmlStr += "<script>"; |
| 11 | htmlStr += "function show(idVal){"; |
| 12 | htmlStr += " var val = \"<table border='1'>\" + document.getElementById(idVal).value + \"</table>\";"; |
| 13 | htmlStr += " document.getElementById('displayId').innerHTML = val;"; |
| 14 | htmlStr += "document.getElementById('displayId').style.display = \"block\";"; |
| 15 | //htmlStr += "alert(idVal);"; |
| 16 | //htmlStr += "alert(val);"; |
| 17 | htmlStr += "}"; |
| 18 | htmlStr += "</script>"; |
| 19 | htmlStr += "</head>"; |
| 20 | htmlStr += "<div id='displayId' style='display:none'></div>"; |
| 21 | htmlStr += "<div>\n<table border='1'>\n"; |
| 22 | console.log("<html><table border='1'>"); |
| 23 | printObjectValues(jsonObj,null,level); |
| 24 | console.log("</table></html>"); |
| 25 | htmlStr += "\n</table>\n</div>"; |
| 26 | //var pattern = new RegExp("\\n","g"); |
| 27 | //htmlstr =htmlStr.replace(pattern,''); |
| 28 | htmlStr += "</html>"; |
| 29 | //console.log(htmlStr); |
| 30 | |
| 31 | //console.dir(htmlObj); |
| 32 | var keyObj={}; |
| 33 | for (var key in htmlObj) { |
| 34 | var idx = key.lastIndexOf("."); |
| 35 | if(idx != -1){ |
| 36 | var str = key.substr(0,idx); |
| 37 | keyObj[str] =''; |
| 38 | }else{ |
| 39 | keyObj[key] =''; |
| 40 | } |
| 41 | //console.log(str); |
| 42 | } |
| 43 | |
| 44 | console.dir(keyObj); |
| 45 | console.log("<ul>"); |
| 46 | for (var key in keyObj) { |
| 47 | console.log("<li>" + key + "</li>"); |
| 48 | } |
| 49 | console.log("</ul>"); |
| 50 | |
| 51 | function tabs(level){ |
| 52 | return ''; |
| 53 | } |
| 54 | function tabsOrig(level){ |
| 55 | var tabs=""; |
| 56 | for(var i=0;i<level;i++){ |
| 57 | tabs += "\t"; |
| 58 | } |
| 59 | return tabs; |
| 60 | } |
| 61 | |
| 62 | function printObjectValues(jsonObj,pkey,level){ |
| 63 | var output=""; |
| 64 | var objectHtmlStr=""; |
| 65 | try{ |
| 66 | for (var key in jsonObj) { |
| 67 | if (jsonObj.hasOwnProperty(key)) { |
| 68 | var v = jsonObj[key]; |
| 69 | if(typeof v === 'object' && Array.isArray(v) === false){ |
| 70 | var nkey=""; |
| 71 | if(pkey != null){ |
| 72 | nkey = pkey + "." + key; |
| 73 | }else{ |
| 74 | nkey = key; |
| 75 | } |
| 76 | var str=printObjectValues(v,nkey,level+2); |
| 77 | }else if(typeof v === 'object' && Array.isArray(v) === true){ |
| 78 | var nkey=""; |
| 79 | if(pkey != null){ |
| 80 | nkey = pkey + "." + key; |
| 81 | }else{ |
| 82 | nkey = key; |
| 83 | } |
| 84 | var str = printArrayValues(v,nkey,level+2); |
| 85 | }else{ |
| 86 | if(pkey != null){ |
| 87 | //console.log(pkey + "." + key + ":" +printValue(v) ); |
| 88 | console.log("<tr><td>" + pkey + "." + key + "</td><td>" +printValue(v) + "</td></tr>" ); |
| 89 | htmlObj[pkey + "." + key ] = printValue(v); |
| 90 | }else{ |
| 91 | //console.log( key + ":" +printValue(v) ); |
| 92 | console.log("<tr><td>" + key + "</td><td>" +printValue(v) + "</td></tr>" ); |
| 93 | htmlObj[key ] = printValue(v); |
| 94 | } |
| 95 | //printValue(v); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | }catch(err){ |
| 100 | console.log(err); |
| 101 | } |
| 102 | return output; |
| 103 | } |
| 104 | |
| 105 | }catch(err){ |
| 106 | console.log( err ); |
| 107 | } |
| 108 | |
| 109 | function printArrayValues(arrObj,pkey,level){ |
| 110 | var output =""; |
| 111 | var arrayHtmlStr =""; |
| 112 | try{ |
| 113 | for(var i=0;arrObj != null && i<arrObj.length;i++){ |
| 114 | var o=arrObj[i]; |
| 115 | if(typeof o === 'object' && Array.isArray(o) === false){ |
| 116 | var nkey = pkey +"[" + i + "]" ; |
| 117 | var str = printObjectValues(o,nkey,level+2); |
| 118 | }else if(typeof o === 'object' && Array.isArray(v) === true){ |
| 119 | var nkey = pkey +"[" + i + "]" ; |
| 120 | var str = printArrayValues(o,nkey,level); |
| 121 | }else{ |
| 122 | console.log("<tr><td>" + pkey + "." + key + "</td><td>" +printValue(o) + "</td></tr>" ); |
| 123 | htmlObj[pkey + "." + key ] = printValue(o); |
| 124 | //console.log(pkey + ":" +printValue(o) ); |
| 125 | //output += tabs(level) + "<tr><td>" + printValue(o) + "</td></tr>\n"; |
| 126 | } |
| 127 | } |
| 128 | }catch(err){ |
| 129 | console.log(err); |
| 130 | } |
| 131 | return output; |
| 132 | } |
| 133 | |
| 134 | function printValue(obj){ |
| 135 | if(obj != undefined){ |
| 136 | if(typeof obj == 'string'){ |
| 137 | return "'" + obj + "'"; |
| 138 | }else{ |
| 139 | return obj; |
| 140 | } |
| 141 | }else{ |
| 142 | return ""; |
| 143 | } |
| 144 | } |