summaryrefslogtreecommitdiffstats
path: root/dgbuilder/public/util/js/jsonTool.js
blob: f4d0de368c005cf979138e30eb57d96f1f02a1c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var obj={};
function dotToJson(str,value,obj){
    //var objArr = path.split("."), part;
    var objArr = str.split(".");
	var prevStr;
	var currObj;
	var prevObj;
	console.log(str);
	for(var i=0;i<objArr.length -1;i++){
		var subStr= objArr[i] ;
		//console.log("subStr:" + subStr);
		if(i==0 && obj[subStr] == undefined ){
			obj[subStr] = {};
			currObj= obj[subStr];
		}else if(i==0 && obj[subStr] != undefined ){
			currObj=obj[subStr];
			//console.log("in gkjgjkg");
		}else{
			if(i == 1){
	 			prevObj=obj[prevStr];
				if(prevObj[subStr] == undefined){
					prevObj[subStr] = {};
					currObj = prevObj[subStr];
				}else{
					currObj = prevObj[subStr];
				}
			}else{
				if(prevObj[subStr] == undefined){
					prevObj[subStr] = {};
					currObj = prevObj[subStr];
				}else{
					currObj = prevObj[subStr];
				}
			}
			//console.dir(currObj);
		}
		prevStr=subStr;
		if(i <objArr.length-2){
			prevObj=currObj;	
		}
	}
	var lastStr = objArr[objArr.length-1];
	currObj[lastStr] = value;
	//prevObj[lastStr] = value;
	//console.dir(currObj);
	return obj;
}
n=dotToJson('abcfdfghdghdghdghdhdhd.efhfjhfjhfjhfjhfjhfjhfbg.hfjhfjhfjhfjhfjhfjh.l',"1234",obj);
//console.dir(n);
JSON.stringify(n,undefined,2);
m=dotToJson('abc.ebg.h.n',"5678",n);
console.dir(m);
function printObj(obj){
for( j in obj){
	console.log(j + ":" + obj[j]);
	if(typeof obj[j] == "object" ){
		printObj(obj[j]);
	}

}
}
printObj(m);
a=JSON.parse(JSON.stringify(m,null,2));
console.dir(a);


//console.log (stringToObj('abc.ebg.h',"",{}));