简体   繁体   English

Node.js JSON嵌套数组对象访问

[英]Node.js JSON nested array object access

I am building an application which GETS JSON response from an API call in SONARQUBE. 我正在构建一个应用程序,该应用程序从SONARQUBE中的API调用获取GETS JSON响应。

Using node js how do I access the value of duplicated_lines from the below JSON object. 使用节点js,如何从下面的JSON对象访问重复的行的值。

I tried the below but always get undefined. 我尝试了以下内容,但始终无法定义。 Is there any library that supports exploring these objects? 有没有支持探索这些对象的库?

Code

var subrequest = unirest("GET",queryUrl);

subrequest.end(function (resXX) {
  if (resXX.error);
    for (var key in resXX) { 
      if (resXX.hasOwnProperty(key)) {
        console.log("Checking the id " + resXX[key].msr.duplicated_lines);
      }
    }
});

JSON JSON

[
  {
    "id": 1933853,
    "uuid": "XXXXXXXXXXXXXX",
    "key": "XXXXXXXXXXXX",
    "name": "XXXXXXX",
    "scope": "PRJ",
    "qualifier": "TRK",
    "date": "2018-02-16T08:07:55-0500",
    "creationDate": "2017-09-20T09:50:05-0400",
    "lname": "XXXXXXXX",
    "version": "15",
    "msr": [
      {
        "key": "duplicated_lines",
        "val": 926192,
        "frmt_val": "926,192"
      },
      {
        "key": "bugs",
        "val": 7467,
        "frmt_val": "7,467"
      },
      {
        "key": "ncloc",
        "val": 1369829,
        "frmt_val": "1,369,829"
      },
      {
        "key": "code_smells",
        "val": 22677,
        "frmt_val": "22,677"
      },
      {
        "key": "vulnerabilities",
        "val": 95,
        "frmt_val": "95"
      }
    ]
  }
]

I don't know anything about the API you are accessing, and your code is hard to interpret in the absence of any other information. 我对您正在访问的API一无所知,在缺少任何其他信息的情况下,您的代码很难解释。 However msr is an array, so you need to access the objects in it with an index: 但是,msr是一个数组,因此您需要使用索引访问其中的对象:

resXX[key].msr.forEach(m => {
    if (m.key === 'duplicated_lines') {
        console.log('Checking the id ' + m.val);
    }
});

I tried the below but always get undefined 我尝试了以下内容,但始终无法定义

Because you are trying to use object value as key while accessing. 因为您试图在访问时将对象value用作key

Try this single line of code : 试试这一行代码:

var duplicateData = jsonObj[0].msr.filter(item => item.key == 'duplicated_lines');

Working Demo 工作演示

 var jsonObj = [ { "id": 1933853, "uuid": "XXXXXXXXXXXXXX", "key": "XXXXXXXXXXXX", "name": "XXXXXXX", "scope": "PRJ", "qualifier": "TRK", "date": "2018-02-16T08:07:55-0500", "creationDate": "2017-09-20T09:50:05-0400", "lname": "XXXXXXXX", "version": "15", "msr": [ { "key": "duplicated_lines", "val": 926192, "frmt_val": "926,192" }, { "key": "bugs", "val": 7467, "frmt_val": "7,467" }, { "key": "ncloc", "val": 1369829, "frmt_val": "1,369,829" }, { "key": "code_smells", "val": 22677, "frmt_val": "22,677" }, { "key": "vulnerabilities", "val": 95, "frmt_val": "95" } ] } ]; var duplicateData = jsonObj[0].msr.filter(item => item.key == 'duplicated_lines'); console.log(duplicateData); 

这是迭代响应JSON数组的单行解决方案

resXX.forEach(item => item.msr.forEach(v => v['key']==='duplicated_lines' && console.log(`Checking the id ${v['val']}`)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM