将 JSON 数据编辑为 Firebase 格式
Posted
技术标签:
【中文标题】将 JSON 数据编辑为 Firebase 格式【英文标题】:Edit JSON data to Firebase format 【发布时间】: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"
【问题讨论】:
【参考方案1】:转换数据格式:
let input = [
"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"
];
let output = ;
input.forEach((row) =>
if (!output[row.country]) output[row.country] = ;
output[row.country][row.tel] = row;
)
console.log(output);
之后,您需要将其写入 Firebase 或文件。
【讨论】:
我添加了此代码,因为赞比亚需要用引号括起来,并且还要将输出从单引号更改为双引号var a =output.toString().replace(/'/g, '"'); var b = JSON.stringify(output); console.log(b);
以上是关于将 JSON 数据编辑为 Firebase 格式的主要内容,如果未能解决你的问题,请参考以下文章
如何从android下载firebase数据库作为CSV文件
如何将 firbease 的 DataSnapshot 转换为 json 文件或如何将 firebase 数据库检索为 json 文件