从一个json获取数据并将其放入另一个json中

时间:2014-11-19 19:30:19

标签: javascript json

我有一个非常大的json文件,并且该json文件内部是一个数组。我想使用JavaScript将json作为参数,通过json解析并仅从中获取某些元素并将其放入新的json文件中,下面是我的意思的一个示例:

{
"contextType": "Account",
"preferences": null,
"custodianCode": null,
"data": [{
"id": "0",
"account": "11111111",
"field2": true,
"field3": false,
"field4": "BROK",
"field5": "Broker",
"field6": "1",
"field7": "Cash"
 },{
 "id": "1",
"account": "222222222",
"field2": true,
"field3": false,
"field4": "BROK",
"field5": "Broker",
"field6": "1",
"field7": "Cash"
}]

}

我想从中获取并获得像这样的新json

{
 "newArray": [{
"id": "0",
"account": "11111111",
"field2": true,
"field3": false,
"field4": "BROK",
"field6": "1"
 },{
"id": "0",
"account": "222222222",
"field2": true,
"field3": false,
"field4": "BROK",
"field6": "1"
}]

}

此外,该文件是我的计算机的本地文件,也可以在本地输出,我正在尝试使用node.js和JavaScript这是我到目前为止所拥有的

 var json = require('./simple.json');
 var keeperFields = ["id", "account", "field2", "field3", "field4", "field6"];
 var newJSON = {newArray: [] };
 var i;
 var fields;

 for (i = 0; i < keeperFields.length; i++) {
   for (fields in json) {
     if (json.hasOwnProperty(keeperFields[i])) {
       newJSON.newArray.push(keeperFields[i]);
       }
      }
     }

   console.log(newJSON);

这只是一个小例子,真正的json文件很庞大,有数千行。任何帮助或建议表示赞赏!

这个当前的解决方案是给我一个{newArray:[]}的控制台日志,而不是上面的预期结果

2 个答案:

答案 0 :(得分:0)

你走了。你需要从json的.data字段开始

&#13;
&#13;
json = {
	"contextType": "Account",
	"preferences": null,
	"custodianCode": null,
	"data": [{
		"id": "0",
		"account": "11111111",
		"field2": true,
		"field3": false,
		"field4": "BROK",
		"field5": "Broker",
		"field6": "1",
		"field7": "Cash"
	},{
		"id": "1",
		"account": "222222222",
		"field2": true,
		"field3": false,
		"field4": "BROK",
		"field5": "Broker",
		"field6": "1",
		"field7": "Cash"
	}]
}

var keeperFields = ["id", "account", "field2", "field3", "field4", "field6"];
var newJSON = {newArray: [] };
json.data.forEach(function(obj) {
	var newObj = {};
	for (var i = 0; i < keeperFields.length; i++) {
		if (obj.hasOwnProperty(keeperFields[i])) {
			newObj[keeperFields[i]] = obj[keeperFields[i]];
		}
	}
	newJSON.newArray.push(newObj);
});

document.write(JSON.stringify(newJSON));
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您知道输入文件的格式是标准化的,则可以更轻松地完成此操作。这样的事情应该有用......

&#13;
&#13;
// assuming you loaded your data into a json object named json and that it contains a field called "data" that contains a list of the records you want to inject into the new array.
var json = require('./simple.json');
var keeperFields = ["id", "account", "field2", "field3", "field4", "field6"];
var newJSON = {
  newArray: []
};

// put the list of records into a convenience var.
var records = json.data;
// loop through the records
for (var recordId in data) {
  // Grab the current record
  var record = records[recordId];
  // Create a new record
  var newRecord = [];
  // Loop through the keeperfields. Since they are named you can create fields in the new record using the same field names
  for (var field in keeperFields) {
    // Copy record data from source record to new record 
    newRecord[field] = record[field];
  }
  // append the new record to the new data set
  newJSON.newArray.push(newRecord);
}
&#13;
&#13;
&#13;

相关问题