遍历嵌套的JSON对象以查找值id

时间:2018-11-29 16:12:48

标签: javascript postman

    "results": {
    "data": {
        "facets": {
            "60749428": {
                "id": 60749428,
                "name": "KC Content Content Kind"
            },
            "60750276": {
                "id": 60750276,
                "name": "KC Content Product Version"
            },
            "69107204": {
                "id": 69107204,
                "name": "KC Video Audience"
            },
            "69127027": {
                "id": 69127027,
                "name": "KC Content Kind ID"
            }
        }
    }
}

我想通过进入facet对象来遍历此嵌套的json对象,并说一下name属性是否为“ KC Content Kind ID”,然后返回该对应name属性的id

因此,在与邮递员进行api调用后,我试图以此方式在我的成功函数中获取“ KC内容类型ID”的对应ID,但是由于它不是数组,我想知道每个对象是否都可以在jquery中工作。

    //Get Available Kinds
function getAvailableKinds() {
    $.ajax({
        url: csexe + "/api/v2/facets/" +getLocationId(),
        dataType: "json",
        type: "GET",
        beforeSend: function(xhr) {
            xhr.setRequestHeader ("OTCSticket", getAuthToken());
        },
        success: function(response) {
            var obj = response.results.data.facets;
            $.each(obj, function(item, value){
                 if ( value == 'KC Content Kind ID') {
                     var idRequired = obj.id;
                 }
            });
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert("An error occurred... Look at the console");
            $("body").html('<p>status code: '+jqXHR.status+'</p><p>Error Thrown: ' + errorThrown + '</p><p>Response Text:</p><div>'+jqXHR.responseText + '</div>');
        }
    });

4 个答案:

答案 0 :(得分:0)

只需解析字符串,然后执行简单的循环即可。

var jsonObj = (JSON.parse("your json here")).data.facets;

for (i = 0; i<jsonObj.length;i++)
{
    if(jsonObj[i].name == "KC Content Kind ID")
        return jsobObj[i].id;
}

答案 1 :(得分:0)

我认为最简单的方法是将Object.values函数与Array.prototype.filter结合使用。然后,您可以从filter方法返回的数组中获取第一项(因为每个ID应该是唯一的)并显示其ID。

const o = { "results": { "data": { "facets": { "60749428": { "id": 60749428, "name": "KC Content Content Kind" }, "60750276": { "id": 60750276, "name": "KC Content Product Version" }, "69107204": { "id": 69107204, "name": "KC Video Audience" }, "69127027": { "id": 69127027, "name": "KC Content Kind ID"}}}}};

const [a] = Object.values(o.results.data.facets).filter(f => f.name == "KC Content Kind ID");

console.log(a.id);

答案 2 :(得分:0)

您可以使用Object.keysfind

const obj = {"results": {"data": {"facets": {"60749428": {"id": 60749428,"name": "KC Content Content Kind"},"60750276": {"id": 60750276,"name": "KC Content Product Version"},"69107204": {"id": 69107204,"name": "KC Video Audience"},"69127027": {"id": 69127027,"name": "KC Content Kind ID"}}}}};
    
const facets = obj.results.data.facets;
const result = Object.keys(facets).find(v => facets[v].name === 'KC Content Kind ID');
//your object keys are equal to id, you can just return key
console.log(result);

// if your object keys can be different from id you can do this
console.log(facets[result].id);

答案 3 :(得分:0)

var obj = {
  "results": {
    "data": {
      "facets": {
        "60749428": {
          "id": 60749428,
          "name": "KC Content Content Kind"
        },
        "60750276": {
          "id": 60750276,
          "name": "KC Content Product Version"
        },
        "69107204": {
          "id": 69107204,
          "name": "KC Video Audience"
        },
        "69127027": {
          "id": 69127027,
          "name": "KC Content Kind ID"
        }
      }
    }
  }
};
let facets = obj.results.data.facets;
let id; 
for(let key in facets){
  if(facets[key].name == 'KC Content Kind ID'){
    id = facets[key].id;
    break;
  }
}
console.log(id);