【问题标题】:JavaScript: Parse JSON with dynamic key valuesJavaScript:使用动态键值解析 JSON
【发布时间】:2018-12-25 20:19:32
【问题描述】:

我有一个如下的 JSON,我想从中提取列和行数组中的数据。

{
  "id":0,
  "tabName":"newtab",
  "columns":[
    {
      "id":0,
      "name":"firstname",
      "type":"entity",
      "operator":"=",
      "$$hashKey":"object:40"
    },
    {
      "id":1,
      "name":"address",
      "type":"domain",
      "operator":"<",
      "$$hashKey":"object:50"
    }
  ],
"rows":[
  {
    "0":"Adam", //this first row data corresponds to "Name" column
    "1":"5432 River Drive", //this first row data corresponds to "Address" column
    "$$hashKey":"object:34",
    "tabId":"1122", 
    "description":"New Tab",
    "id":1
  },
  {
    "0":"Silver",  //this 2nd row data corresponds to "Name" column
    "1":"Address 2", //this 2nd row data corresponds to "Address" column
    "$$hashKey":"object:53",
    "tabId":"2345",
    "description":"New 2nd Tab",
    "id":2
  }
],
"uniqueIdCounter":3
}

从上面的 JSON 中,我想提取列和行的值。这只是示例 JSON,实际的 JSON 可以有更多的列和行条目。

在列数组中: 每个列条目的 id、name、type 运算符键保持相同。

在行数组中: tabid、description 和 id 键对于每个行条目保持相同。

行数组的问题在于键值“0”、“1”(实际上对应于列名和地址)。如何遍历这个数组并提取这些动态值?动态是指如果我有超过 2 行,则行中的键将是“0”、“1”、“2”等其他行。

我开始写这个循环:

var jsonColumns = JSON.parse(JSON.stringify($scope.myTable.columns));
for (var i = 0; i < jsonColumns.length; i++) {
    var obj = jsonColumns[i]; 
    //For columns data I can do obj.id, obj.name etc and works fine
}

对于行,我不确定如何在此处继续:

var jsonRows = JSON.parse(JSON.stringify($scope.myTable.rows));
for (var i = 0; i < jsonRows.length; i++) {
    var obj = jsonRows[i]; 
    //How can I extract rows with keys "0", "1" where I dont know these key names in advance
}

请对此有任何意见吗?谢谢

【问题讨论】:

  • 你可以做 Object.getOwnPropertyNames(jsonRows[i]) 来获取所有的键。

标签: javascript angularjs json


【解决方案1】:

为每个循环使用它:

var jsonData = {
    data: [{
        "key1": 1,
        "key2": 2
    }, {
        "key1": 3,
        "key2": 4
    }]
}
for (var index in jsonData.data) {
    for (var key in jsonData.data[index]) {
        console.log(key)
    }
}

【讨论】:

    【解决方案2】:

    您可以像这样提取密钥:

    var jsonRows = JSON.parse(JSON.stringify($scope.myTable.rows));
    for (var i = 0; i < jsonRows.length; i++) {
         var obj = jsonRows[i]; 
         console.log("Rows", obj["0"], obj["1"], obj["id"], obj["tabId"], obj["description"]); //here you can extract data with specified keys
    }
    

    您的其余代码将保持不变。

    【讨论】:

      【解决方案3】:

      假设 tabid、description、id 和 $$hashKey 是静态的,您可以按如下方式提取这些动态键,

      let tableObj = {
        "id":0,
        "tabName":"newtab",
        "columns":[
          {
            "id":0,
            "name":"firstname",
            "type":"entity",
            "operator":"=",
            "$$hashKey":"object:40"
          },
          {
            "id":1,
            "name":"address",
            "type":"domain",
            "operator":"<",
            "$$hashKey":"object:50"
          }
        ],
      "rows":[
        {
          "0":"Adam", //this first row data corresponds to "Name" column
          "1":"5432 River Drive", //this first row data corresponds to "Address" column
          "$$hashKey":"object:34",
          "tabId":"1122", 
          "description":"New Tab",
          "id":1
        },
        {
          "0":"Silver",  //this 2nd row data corresponds to "Name" column
          "1":"Address 2", //this 2nd row data corresponds to "Address" column
          "$$hashKey":"object:53",
          "tabId":"2345",
          "description":"New 2nd Tab",
          "id":2
        }
      ],
      "uniqueIdCounter":3
      };
      
      let jsonRows = JSON.parse(JSON.stringify(tableObj.rows));
      let staticAttributes = ['tabId', 'description', 'id', '$$hashKey'];
      let extractedData = [];
      for (const obj of jsonRows) {
          let extractedObj = {};
          
          for (let key in obj) {
            if (obj.hasOwnProperty(key) && !staticAttributes.includes(key)) {
              extractedObj[key] = obj[key];
            }
          }
          
          extractedData.push(extractedObj);
      }
      
      console.log(extractedData);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        • 2021-05-18
        • 2011-02-22
        相关资源
        最近更新 更多