gpt4 book ai didi

java - 在 Jquery 中基于 KeyPair 循环 json 数据

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:18 25 4
gpt4 key购买 nike

大家好,下面是我从服务器收到的响应。我尝试根据按键循环它,不幸的是我没有成功。

{"metricsLevelList": [{
"levelName": "Account1Name",
"levelId": 1,
"metrics": [
{
"value": "80",
"greenZoneStart": "90",
"greenZoneEnd": "100",
"yellowZoneStart": "60",
"yellowZoneEnd": "90",
"redZoneStart": "0",
"redZoneEnd": "60",
"metricsType": "Dial",
"metricsName": "FTR Deliverables"
},
{
"value": "0",
"greenZoneStart": "90",
"greenZoneEnd": "100",
"yellowZoneStart": "60",
"yellowZoneEnd": "90",
"redZoneStart": "0",
"redZoneEnd": "60",
"metricsType": "Dial",
"metricsName": "OTD Deliverables"
},
{
"value": "0",
"greenZoneStart": "0",
"greenZoneEnd": "5",
"yellowZoneStart": "5",
"yellowZoneEnd": "15",
"redZoneStart": "15",
"redZoneEnd": "100",
"metricsType": "Dial",
"metricsName": "% Defect Rejection"
},
{
"value": null,
"greenZoneStart": null,
"greenZoneEnd": null,
"yellowZoneStart": null,
"yellowZoneEnd": null,
"redZoneStart": null,
"redZoneEnd": null,
"metricsType": null,
"metricsName": null
},
{
"value": "0",
"greenZoneStart": "90",
"greenZoneEnd": "100",
"yellowZoneStart": "70",
"yellowZoneEnd": "90",
"redZoneStart": "0",
"redZoneEnd": "70",
"metricsType": "Dial",
"metricsName": "CSAT (% VoC)"
}
]
}]}

在这里,我必须在另一个指标循环(具有指标内容)内循环指标级别列表(其中我将有不同的级别名称)。请有人帮我解决这个问题。

最佳答案

您可以使用以下循环来迭代嵌套对象(假设您的 json 存储在名为 jsonDemo 的变量中):

function loop(obj) {
$.each(obj, function(key, val) {
if(val && typeof val === "object") { // object, call recursively
console.log(" ");
loop(val);
} else {
console.log(key + "-->" + obj[key]);
}
});
}

loop(jsonDemo);

参见 fiddle :“http://jsfiddle.net/pgo637dj/

说明:这将迭代对象的所有键并检查每个键是否是一个对象,如果是,则递归调用它,否则将显示它。

关于java - 在 Jquery 中基于 KeyPair 循环 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31474496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com