【问题标题】:Edit JSON data to Firebase format将 JSON 数据编辑为 Firebase 格式
【发布时间】: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


    【解决方案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);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2017-12-06
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    相关资源
    最近更新 更多