我有一个名为data.json的文件,其数据如下所示。它来自firebase数据库。
[
"-L1PW1Gt_34w1xb32BK5" : {
"Article_Headline" : "Mobay flood victims receive help from ministry",
"Article_Image_URL" : "https://www.wefornews.com/wp-content/uploads/2017/01/news-3.jpg",
"Article_Published_Date" : "Wed, 27 Dec 2017 23:02:53 GMT",
"Article_URL" : "http://www.jamaicaobserver.com/latestnews/Mobay_flood_victims_receive_help_from_ministry?profile=1228",
"Category" : "riots,",
"Location" : "Jamaica",
"Source_Name" : "jamaica observer",
"Tags" : "SocialSecurity,LabourParty,MontegoBay,Horsepower,JIS,Jamaica,USRoute36,AndrewHolness"
},
]
我希望我的数据看起来像这样
{
"Article_Headline" : "Mobay flood victims receive help from ministry",
"Article_Image_URL" : "https://www.wefornews.com/wp-content/uploads/2017/01/news-3.jpg",
"Article_Published_Date" : "Wed, 27 Dec 2017 23:02:53 GMT",
"Article_URL" : "http://www.jamaicaobserver.com/latestnews/Mobay_flood_victims_receive_help_from_ministry?profile=1228",
"Category" : "riots,",
"Location" : "Jamaica",
"Source_Name" : "jamaica observer",
"Tags" : "SocialSecurity,LabourParty,MontegoBay,Horsepower,JIS,Jamaica,USRoute36,AndrewHolness"
"key": "-L1PW1Gt_34w1xb32BK5"
}
可以使用哪些代码来产生这样的结果?
以下是我的代码无效
var http = require('http');
var fs = require('fs');
var obj = fs.readFileSync('./data.json', 'utf8');
function snapshotToArray(obj) {
var returnArr = [];
obj.forEach(function(childSnapshot) {
var item = childSnapshot.val();
item.key = childSnapshot.key;
returnArr.push(item);
});
return returnArr;
};
console.log(snapshotToArray(obj));
答案 0 :(得分:0)
通常,鉴于JSON有效:
import fs from 'fs';
const obj = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
const data_to_keep = obj.the_key_of_data_to_keep;
delete obj.the_key_of_data_to_keep;
现在,属性the_key_of_data_to_keep
已被删除,但您仍然可以通过data_to_keep
引用其值,例如,您可以在obj
中的某处插入。