[英]JavaScript: Parse JSON with dynamic key values
我有一個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,實際的可以包含更多的列和行條目。
在columns數組中:id,name,type操作符對於每個列條目保持不變。
在rows數組中:每個行條目的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
}
對此有什么投入嗎? 謝謝
假設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);
您可以像這樣提取密鑰:
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
}
您其余的代碼將相同。
用於每個循環:
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) } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.