我正在做一个小项目。我坚持一个小的 js 问题。 我有像这样的json字符串:
var jsObj = {
"templates": {
"form0": {
"ID": "MyAlertNew",
"isVisible": "true",
"children": [
{
"-type": "kButton3",
"ID": "myButtonID1",
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "dynamic text in file"
},
{
"-type": "kButton1",
"ID": "btnAlign2",
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
}
}
]
},
"form1": {
"ID": "frmNewPOC1",
"isVisible": "true",
"children": [
{
"-type": "kButton3",
"ID": "btnAlign",
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "in diff form"
},
{
"-type": "kButton1",
"ID": "btnAlignTest",
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
},
"text": "in My Form"
}
]
}
}
};
我想像这样过滤它:
objnew ={
"MyAlertNew":{
"isVisible": "true"
},
"myButtonID1":{
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "dynamic text in file"
},
"btnAlign2":{
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
}
},
"frmNewPOC1":{
"isVisible": "true"
},
"btnAlign":{
"isVisible": "true",
"onClick": "onClickMethod",
"paddings": {
"left": "0",
"right": "0",
"top": "3",
"bottom": "3",
"unit": "%"
},
"text": "in diff form"
},
"btnAlignTest":{
"visible": "true",
"margins": {
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"unit": "%"
},
"text": "in My Form"
}
}
到目前为止,我已经尝试过使用下面的代码,但我获得了成功。
testObj = jsObj.templates;
var idObj = [];
var widgetProp =[];
var ID ;
function parseMyObj(testObj){
for(x in testObj){
if(typeof testObj[x] == "object"){
//widgetProp[]
parseMyObj(testObj[x]);
}
else if(x=='ID'){
ID = testObj[x];
idObj.push(testObj[x]);
}
else{
widgetProp.push(x:testObj[x]);
}
}
}
parseMyObj(testObj);
console.log(widgetProp);
console.log(JSON.stringify(idObj));
请帮助我提前谢谢。