获取json中所有的text键值,json文件里面就是这样的,该怎么获取啊,急...
发布网友
发布时间:2024-04-29 21:50
我来回答
共4个回答
热心网友
时间:2024-10-08 22:23
var json = [{
"text": "ManagedElement",
"children": [{
"text": "ENodeBFunction",
"children": [{
"text": "AdmissionControl"
}, {
"text": "AnrFunction",
"children": [{
"text": "AnrFunctionEUtran"
}, {
"text": "AnrFunctionGeran"
}, {
"text": "AnrFunctionUtran"
}]
}, {
"text": "DrxProfile"
}, {
"text": "EUtranCellTDD",
"children": [{
"text": "EUtranFreqRelation",
"children": [{
"text": "EUtranCellRelation"
}]
}, {
"text": "GeranFreqGroupRelation",
"children": [{
"text": "GeranCellRelation"
}, {
"text": "ReportConfigA1Prim"
}, {
"text": "ReportConfigA1Sec"
}, {
"text": "ReportConfigA4"
}, {
"text": "ReportConfigA5",
"children": [{
"text": "ReportConfigA5Anr"
}]
}]
}]
}]
}]
}];
var isArray = function(json) {
return Object.prototype.toString.call(json) === "[object Array]" ? true : false;
}
var result = [];
var yugi = function(json) {
if (isArray(json)) {
for (var i = 0; i < json.length; i++) {
var tmp = json[i];
if (!isArray(tmp)) {
for (var k in tmp) {
var tk = tmp[k];
if (isArray(tk)) {
yugi(tk);
} else if (k == "text") {
result.push(tk);
}
}
} else {
yugi(tmp);
}
}
} else {
for (var k in json) {
var tk = json[k];
if (isArray(tk)) {
yugi(tk);
} else if (k == "text") {
result.push(tk);
}
}
}
}
yugi(json);
console.log(result);
for(var i = 0; i < result.length; i++){
console.log(result[i] + " 是第 " + (i + 1) + " 个");
}
回答完毕,采纳即可!!!!!!!!!
热心网友
时间:2024-10-08 22:27
454269563 加入群可以结识更多的牛人!!
热心网友
时间:2024-10-08 22:22
上这里(landingbj技术BBS)看看,解决你的IT难题!
热心网友
时间:2024-10-08 22:23
描述不清……