【问题标题】:How to filter and retrieve nested JSON data如何过滤和检索嵌套的 JSON 数据
【发布时间】:2020-11-21 11:15:52
【问题描述】:

我有以下 JSON 数据结构。我正在寻找一个可以返回与特定“ID”关联的 X Y Z 值的函数。我在 NodeJs 中工作,并且一直在使用 parsedData = JSON.parse(JSONdata)。 Havent 经常解析 JSON 数据,所以我很困惑解决此类问题的正确方法是什么。

"RTLSTranspondersOption4": 
        [{
            "ID": "a",
            "X": "1",
            "Y": "2",
            "Z": "3",
        }, {
            "ID": "b",
            "X": "4",
            "Y": "5",
            "Z": "6"
        }, {
            "ID": "c",
            "X": "7",
            "Y": "8",
            "Z": "9"
        }]

我希望创建如下函数:

let configJSON2 = fs.readFileSync('TestConfig.JSON').toString()
let parsedData = JSON.parse(configJSON2)

getData(ID, desiredAxis){
 return parsedData.filter...????
}

示例:getData(b, X) 返回 4。

我正在创建这个 JSON 数据的结构,以便我可以重新组织它以适应最有意义的任何结构,因此请随时就更有效的数据结构方式提出建议。

也欢迎任何与此主题相关的其他资源。

请求的整个 JSON:

{
    "MindSphereRSA": {
        "content": {
          "baseUrl": "https://southgate.eu1.mindsphere.io",
          "iat": "X",
          "clientCredentialProfile": [
            "RSA_3072"
          ],
          "clientId": "X",
          "tenant": "X"
        },
        "expiration": "X"
      },
      "MindSpherePrivateKey": "X",
      "RTLSDatabaseConnectionString": {
        "user": "X",
        "password": "X",
        "server": "X",
        "database": "X",
        "encrypt": false 
    },
      "RTLSTranspondersOption4": 
        [{
            "ID": "a",
            "X": "1",
            "Y": "2",
            "Z": "3",
        }, {
            "ID": "b",
            "X": "4",
            "Y": "5",
            "Z": "6"
        }, {
            "ID": "c",
            "X": "7",
            "Y": "8",
            "Z": "9"
        }]

      
}

【问题讨论】:

  • 发布你的整个 JSON
  • 已编辑以包含整个 JSON。
  • parsedData 包含什么
  • 解析的数据包含readFileSync('JSONdata.JSON').tostring()的结果
  • 让 configJSON2 = fs.readFileSync('TestConfig.JSON').toString() 让 parsedData = JSON.parse(configJSON2)

标签: javascript node.js json parsing


【解决方案1】:
getData(ID, desiredAxis){
  const item = parsedData.find(i => i.ID === ID);
  return item ? item[desiredAxis] : undefined;
}

【讨论】:

  • 谢谢,但由于 parsedData.find,我似乎收到了一个错误。我得到以下信息:``` const item = parsedData.find(i => i.ID === ID); ^ TypeError: parsedData.find is not a function at getData (C:\Users\z0043rkh\Desktop\Rotation 3\node-course\notes-app\app.js:12:29) at ```
  • 请阅读How to Answer。仅代码的答案可能对 OP 或此答案的未来查看者没有太大帮助。好的答案包括对为什么这个答案有效以及OP 出了什么问题的一些解释
【解决方案2】:

使用Array.prototype.find 根据指定的IDdesiredAxis 过滤特定对象。

const parsedData = {
    "MindSphereRSA": {
        "content": {
          "baseUrl": "https://southgate.eu1.mindsphere.io",
          "iat": "X",
          "clientCredentialProfile": [
            "RSA_3072"
          ],
          "clientId": "X",
          "tenant": "X"
        },
        "expiration": "X"
      },
      "MindSpherePrivateKey": "X",
      "RTLSDatabaseConnectionString": {
        "user": "X",
        "password": "X",
        "server": "X",
        "database": "X",
        "encrypt": false 
    },
      "RTLSTranspondersOption4": 
        [{
            "ID": "a",
            "X": "1",
            "Y": "2",
            "Z": "3",
        }, {
            "ID": "b",
            "X": "4",
            "Y": "5",
            "Z": "6"
        }, {
            "ID": "c",
            "X": "7",
            "Y": "8",
            "Z": "9"
        }]
};

const getData = function(options, ID, desiredAxis) {
    const foundObj = options.find((option) => option.ID === ID && Object.keys(option).includes(desiredAxis));
    return foundObj[desiredAxis] || null;
};

console.log(getData(parsedData.RTLSTranspondersOption4, 'b', 'X'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-11-28
    • 2021-07-18
    相关资源
    最近更新 更多