【发布时间】:2018-06-24 12:31:59
【问题描述】:
我有一种格式的 json 数据,需要编辑成 Firebase 支持的格式。我创建了一个带有尝试编辑数据格式的功能的 javascript 应用程序,但似乎不起作用。这是一个大量的数据,但下面是一个示例。
app.js 应用程序
var fs = require('fs');
//read data from file
var readData = fs.readFileSync('readMe.json','utf8');
var sample = JSON.parse(readData);
var newData =addFirebaseKey(sample);
// output datda to output.json
fs.writeFileSync('output.json',JSON.stringify(newData),'utf8');
function addFirebaseKey(contacts){
var fireContacts="";
for(var i=0;i<contacts.length;i++){
var tempObj=contacts[i];
//tel number is also the firebase key
var fireKey =tempObj.tel;
fireContacts=fireContacts+fireKey+tempObj;
}
return fireContacts;
}
readMe.json 文件
[
{
"contactName": "Office of the President",
"description": "office of president",
"fax": "-",
"location": "Lusaka",
"postalAddress": "-",
"tel": "123456",
"country": "Zambia"
},
{
"contactName": "State House",
"description": "State president, Ministry of",
"fax": "-",
"location": "Lusaka",
"postalAddress": "-",
"tel": "444900",
"country": "Zambia"
},
{
"contactName": "National Strategy Office",
"description": "national strategy",
"fax": "-",
"location": "Gaborone",
"postalAddress": "-",
"tel": "222222",
"country": "Zambia"
}
]
output.json 文件
"123456[object Object]444900[object Object]222222[object Object]"
这是 Firebase 所需的格式
{
"contactDetail" : {
"Zambia" : {
"123456" : {
"contactName" : "Shocks & Exhaust Fitment Centre",
"description" : "motor vehicle exhaust systems",
"fax" : "-",
"location" : "Lusaka",
"postalAddress" : "NA",
"tel" : "123456"
},
"888555" : {
"contactName" : "K Media",
"description" : "internet marketing",
"fax" : "-",
"location" : "Lusaka",
"postalAddress" : "P O Box 26249, Gaborone",
"tel" : "888555"
},
"555544" : {
"contactName" : "Shocks & Exhaust Fitment Centre",
"description" : "secretarial services",
"fax" : "-",
"location" : "Lusaka",
"postalAddress" : "NA",
"tel" : "555544"
}
}
}
}
【问题讨论】:
标签: javascript json node.js firebase firebase-realtime-database