使用hasOwnProperty的JavaScript/JSON递归逻辑问题
这是JSON,具体问题是注释使用hasOwnProperty的JavaScript/JSON递归逻辑问题,javascript,jquery,json,Javascript,Jquery,Json,这是JSON,具体问题是注释//当前未被解析 var oarsObject = [{ "coordinateReferenceSystem": "26782,15851 <-- not in a value", "positionReferenceType": "geogWgs84", "geogWgs84": { "latitude": { "value": 0.50018514607964015
//当前未被解析
var oarsObject = [{
"coordinateReferenceSystem": "26782,15851 <-- not in a value",
"positionReferenceType": "geogWgs84",
"geogWgs84": {
"latitude": {
"value": 0.50018514607964015
},
"longitude": {
"value": -1.587237735514361
},
"height": {
"value": 0.0
}
}
}, {
"coordinateReferenceSystem": {
"value": "26782,15851 <-- was in a value"
},
"positionReferenceType": {
"value": "geogWgs84"
},
"geogWgs84": {
"latitude": {
"value": 0.5001841257180204
},
"longitude": {
"value": -1.5869069927615211
},
"height": {
"value": 0.0
}
}
}, {
"coordinateReferenceSystem": "26782,15851",
"positionReferenceType": "geogWgs84",
"geogWgs84": {
"latitude": {
"value": 0.49997645018241949
},
"longitude": {
"value": -1.5869078356348387
},
"height": {
"value": 0.0
}
}
}, {
"coordinateReferenceSystem": "26782,15851",
"positionReferenceType": "geogWgs84",
"geogWgs84": {
"latitude": {
"value": 0.4999774704207946
},
"longitude": {
"value": -1.5872385394554729
},
"height": {
"value": 0.0
}
},
"nest": {
"level_1": {
"child_1": {
"value": "child_1 <-- level 1",
"gchild_1": {"value": "gchild_1 <-- level 2"} // doesn't get parsed currently
},
"child_2": {
"gchild_1": {
"value": "gchild_1 <-- level 2"
}
},
"child_3": {
"gchild_2": {
"ggchild_1": {
"value": "ggchild_1 <-- level 3"
}
}
}
}
}
}];
var-oarsObject=[{
“协调引用系统”:“2678215851您可以使用
function propertyTest(currentObject) {
for (var property in currentObject) {
if (currentObject[property].hasOwnProperty('value')) {
$('#method1').append(property + ', ' + currentObject[property].value + '<br />');
}
if (typeof currentObject[property] === "object") {
propertyTest(currentObject[property]);
} else if(property != 'value') {
$('#method1').append(property + ' ' + currentObject[property] + '<br />');
}
}
}
函数属性测试(currentObject){
for(currentObject中的var属性){
if(currentObject[property].hasOwnProperty('value')){
$(“#method1”).append(property+”,“+currentObject[property].value+”
);
}
if(currentObject[属性]=“对象”的类型){
propertyTest(currentObject[属性]);
}else if(属性!=“值”){
$(“#method1”).append(property+''+currentObject[property]+'
);
}
}
}
我将简化为:
function propertyTest(currentObject, key) {
for (var property in currentObject) {
if (typeof currentObject[property] === "object") {
propertyTest(currentObject[property], property);
} else {
$('#method1').append(
(property=='value' && key ? key+',' : property)
+ ' ' + currentObject[property] + '<br />'
);
}
}
}
函数属性测试(currentObject,键){
for(currentObject中的var属性){
if(currentObject[属性]=“对象”的类型){
propertyTest(currentObject[property],property);
}否则{
$('#方法1')。追加(
(属性=='value'&&key?key+',':属性)
+''+currentObject[属性]+'
'
);
}
}
}
谢谢!这很有道理。
function propertyTest(currentObject, key) {
for (var property in currentObject) {
if (typeof currentObject[property] === "object") {
propertyTest(currentObject[property], property);
} else {
$('#method1').append(
(property=='value' && key ? key+',' : property)
+ ' ' + currentObject[property] + '<br />'
);
}
}
}