【问题标题】:How to access values of dictionary of array of dictionary of array using node js?如何使用节点js访问数组字典数组字典的值?
【发布时间】:2020-06-01 10:12:30
【问题描述】:

我有一些单调的 Json 响应,其中我正在尝试使用 node.js 访问数组字典数组的字典元素,但运气不好,无法这样做。我已尝试访问此 stackoverflow 解决方案https://stackoverflow.com/questions/30448517/node-js-how-to-access-values-of-dictionary-within-an-array-of-a-key-in-a-dicti,但在我的情况下,这无法正常工作。我的 Json 响应是:

{
  "client": [
    {

      "code": [
        {
          "id": "001",
          "code": "100",
          "type": "New"
        },
        {
          "id": "002",
          "code": "200",
          "type": "Old"
        }
      ]
    }
  ]
}

从这个响应中,我想获取每个“id”、“code”和“Type”的值。任何有价值的答案将不胜感激。谢谢!!!

【问题讨论】:

    标签: arrays node.js dictionary for-loop jsonresult


    【解决方案1】:

    如果你的 json 被赋值给变量 x

     x = {
       "client": [
         {
    
           "code": [
             {
               "id": "001",
               "code": "100",
               "type": "New"
             },
             {
               "id": "002",
               "code": "200",
               "type": "Old"
             }
           ]
         }
       ]
     }
    
    console.log(x["client"][0]["code"][0]["id"])
    

    (我将遍历数组作为练习留给读者)

    【讨论】:

    • 我刚刚尝试了您的解决方案,但最终出现错误:“TypeError: Cannot read property '0' of undefined”
    • 将代码块复制并粘贴到 node.js 终端窗口中。它应该打印出 001
    猜你喜欢
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    相关资源
    最近更新 更多