【问题标题】:Want to remove outer array of json data想要删除 json 数据的外部数组
【发布时间】:2020-05-06 20:40:53
【问题描述】:

这是我的 json 数据,我想删除外部数组。

   [ 
       [ 
          { 
             "name":"name",
             "zip_code":"50700",
             "state":"state",
             "country":"country"
          }
       ]
    ]

我想要这样

  [ 
      { 
         "name":"name",
         "zip_code":"50700",
         "state":"state",
         "country":"country"
      }
   ]

【问题讨论】:

  • 那么解析它并得到[0]th 元素?

标签: javascript jquery json ajax api


【解决方案1】:

可以简单的获取数组的第一个元素,所以数组的[0]。


// If you have your array
const myArray =   [ 
       [ 
          { 
             "name":"name",
             "zip_code":"50700",
             "state":"state",
             "country":"country"
          }
       ]
    ]

// This will  give you what you want.
myArray[0]

【讨论】:

    【解决方案2】:

    如果您无法将 JSON 更改为不返回嵌套数组,则只需定位数组的第 0 个索引。代码示例:

    var jsonData = [
        [
            {
                Name: 'John Doe'
            }
        ]
    ]
    
    var strippedJsonData = jsonData[0];
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多