当前位置: 代码迷 >> java >> 在KeyPair基础上的Jquery中循环json数据
  详细解决方案

在KeyPair基础上的Jquery中循环json数据

热度:13   发布时间:2023-07-31 11:04:36.0

嗨,大家好,下面是我从服务器得到的答复。 不幸的是,我尝试根据键来循环播放,但没有成功。

{"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)"
  }
]
}]}

在这里,我必须在另一个度量标准循环(其中包含度量标准内容)内循环metricsLevelList(其中我将具有不同的等级名称)。 有人请帮我。

您可以使用以下循环来迭代嵌套的对象(假设您的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);

参见小提琴:“ ”

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

经过大量的尝试和错误我得到了答案

var responseEle =${response};
var importantObject = responseEle.metricsLevelList[0];
for (var item in importantObject) {
var theDate = item; //the KEY
var theNumber = importantObject[item]; //the VALUE
if(item =='metrics')
{
$.each(theNumber, function(i, newItem){

});
}
}
  相关解决方案