【发布时间】:2020-07-17 04:44:28
【问题描述】:
我在另一篇文章中看到有人识别 json 是否嵌套,如果它在第一个对象之后包含第二个对象,那么它将在 JavaScript 中返回 true 如果不是 false
这是他们的方法。我们可以使用 C# 来实现吗
function check_if_nested(obj) {
check_nest=[]
obj.map(function(e,i) {$.each(e, function(v){
if(typeof(obj[0][v])=='object') {check_nest.push('nested')} else {check_nest.push('not nested')}
})})
if(check_nest.includes('nested')) {return(true)} else {return(false)}
}
//Not nested
obj_1 = [{
one: "apples",
two: "oranges"
}]
Usage: check_if_nested(obj_1)
output: false
obj_2 = [{
one: "apples",
two: "oranges",
children: [{
three: "bananas",
four: "jicamas"
}]
}]
output: true
【问题讨论】: