Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | var obj={}; |
| 2 | function dotToJson(str,value,obj){ |
| 3 | //var objArr = path.split("."), part; |
| 4 | var objArr = str.split("."); |
| 5 | var prevStr; |
| 6 | var currObj; |
| 7 | var prevObj; |
| 8 | //console.log(str); |
| 9 | var isArray = false; |
| 10 | var prevObjIsArray = false; |
| 11 | for(var i=0;i<objArr.length -1;i++){ |
| 12 | var subStr= objArr[i] ; |
| 13 | if(isArray){ |
| 14 | prevObjIsArray = true; |
| 15 | } |
| 16 | isArray = false; |
| 17 | if(subStr.indexOf(']') == subStr.length -1){ |
| 18 | subStr = subStr.substring(0,subStr.length -2); |
| 19 | isArray = true; |
| 20 | } |
| 21 | //console.log("subStr:" + subStr + isArray); |
| 22 | //console.dir(prevObj); |
| 23 | if(isArray){ |
| 24 | if(i==0 && obj[subStr] == undefined ){ |
| 25 | //console.log("i==0 && obj[subStr] "); |
| 26 | obj[subStr]=[]; |
| 27 | }else if(i==0 && obj[subStr][0] == undefined ){ |
| 28 | obj[subStr][0]={}; |
| 29 | }else if(i==0 && obj[subStr][0] != undefined ){ |
| 30 | currObj= obj[subStr][0]; |
| 31 | }else{ |
| 32 | if(i == 1){ |
| 33 | //console.log("i==1 && obj[prevStr] "); |
| 34 | prevObj=obj[prevStr]; |
| 35 | if(prevObj[subStr][0] == undefined){ |
| 36 | prevObj[subStr] = []; |
| 37 | prevObj[subStr][0] = {}; |
| 38 | currObj = prevObj[subStr][0]; |
| 39 | }else{ |
| 40 | currObj = prevObj[subStr][0]; |
| 41 | } |
| 42 | }else{ |
| 43 | if(prevObj[subStr] == undefined){ |
| 44 | prevObj[subStr]=[]; |
| 45 | prevObj[subStr][0]={}; |
| 46 | currObj = prevObj[subStr][0]; |
| 47 | }else{ |
| 48 | currObj = prevObj[subStr][0]; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | }else{ |
| 53 | if(i==0 && obj[subStr] == undefined ){ |
| 54 | obj[subStr] = {}; |
| 55 | currObj= obj[subStr]; |
| 56 | }else if(i==0 && obj[subStr] != undefined ){ |
| 57 | currObj=obj[subStr]; |
| 58 | //console.log("in gkjgjkg"); |
| 59 | }else{ |
| 60 | if(i == 1){ |
| 61 | prevObj=obj[prevStr]; |
| 62 | if(prevObj[subStr] == undefined){ |
| 63 | prevObj[subStr] = {}; |
| 64 | currObj = prevObj[subStr]; |
| 65 | }else{ |
| 66 | currObj = prevObj[subStr]; |
| 67 | } |
| 68 | }else{ |
| 69 | if(prevObj[subStr] == undefined){ |
| 70 | prevObj[subStr] = {}; |
| 71 | currObj = prevObj[subStr]; |
| 72 | }else{ |
| 73 | currObj = prevObj[subStr]; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | prevStr=subStr; |
| 79 | if(i <objArr.length-2){ |
| 80 | //console.dir(currObj); |
| 81 | prevObj=currObj; |
| 82 | } |
| 83 | } |
| 84 | var lastStr = objArr[objArr.length-1]; |
| 85 | if(isArray){ |
| 86 | currObj[lastStr] = value; |
| 87 | }else{ |
| 88 | currObj[lastStr] = value; |
| 89 | } |
| 90 | //prevObj[lastStr] = value; |
| 91 | //console.dir(currObj); |
| 92 | return obj; |
| 93 | } |
| 94 | function printObj(obj){ |
| 95 | for( j in obj){ |
| 96 | console.log(j + ":" + obj[j]); |
| 97 | if(typeof obj[j] == "object" ){ |
| 98 | printObj(obj[j]); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | |
Dan Timoney | b217558 | 2017-03-31 17:47:19 -0400 | [diff] [blame] | 104 | var a=[]; |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 105 | var nObj={}; |
| 106 | for(var i=0;i<a.length;i++){ |
| 107 | dotToJson(a[i],'',nObj); |
| 108 | } |
Dan Timoney | b217558 | 2017-03-31 17:47:19 -0400 | [diff] [blame] | 109 | |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 110 | var nObj={}; |
| 111 | for(var i=0;i<a.length;i++){ |
| 112 | var key =a[i].substring(0,a[i].indexOf(':')); |
| 113 | console.log(key); |
| 114 | var value =a[i].substring(a[i].indexOf(':') +1); |
| 115 | if(value == undefined) value =""; |
| 116 | dotToJson(key,value,nObj); |
| 117 | } |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 118 | console.log(JSON.stringify(nObj,null,4)); |
| 119 | //console.log (stringToObj('abc.ebg.h',"",{})); |