JavaScript:使用动态键值解析JSON

时间:2018-07-18 03:19:54

标签: javascript angularjs json

我有一个如下的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可以具有更多的列和行条目。

在columns数组中: id,名称,类型操作符对于每个列条目保持不变。

在rows数组中: 每个行条目的tabid,描述和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
}

请问对此有何投入?谢谢

3 个答案:

答案 0 :(得分:2)

假设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);

答案 1 :(得分:0)

您可以这样提取密钥:

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
}

其余代码相同。

答案 2 :(得分:0)

用于每个循环:

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)
    }
}