简体   繁体   中英

How do I create a JSON string from data stored in Google Firebase

I need my Google Firebase Database Data converted to a JSON string.

在此处输入图片说明

The desired outcome should be as follows:

var dataSet =[
{arr:"test",des:"DMM",eta:"17 Feb 2018 11:00",etd:"17 Feb 2018 13:30",gate:"S92", inbound:"RT456", org:"ARN", outbound:"RT678", remarks:"CHARTER", sta:"17 Feb 2018 11:00", std:"17 Feb 2018 13:30", whs:"T11"},
{arr:"test",des:"ESB",eta:"17 Feb 2018 09:00",etd:"17 Feb 2018 15:30",gate:"S94", inbound:"SD941", org:"JNB", outbound:"SD942", remarks:"", sta:"17 Feb 2018 09:00", std:"17 Feb 2018 15:30", whs:"T11"}
             ];

I'm new to this and have therefore no clue how to accomplish this.

The JSON data is available at https://your-project.firebaseio.com/.json . You can obtain this by sending a GET request to this url. If you have classes in your data and would like to get the data for a particular class instead, use https://your-project.firebaseio.com/your-class.json

You can test this by using curl https://your-project.firebaseio.com/.json For better readability you can use curl https://your-project.firebaseio.com/.json?print=pretty

Refer: https://firebase.google.com/docs/database/rest/retrieve-data

First get the DataSet-schedule object and then iterate for each of the objects inside it :

dbRef.child("DataSet-schedule").once("value").then(
    function(snapshot){
        var dataSetObj = snapshot.val(), text= "";
        for(x in dataSetObj){
            text += JSON.stringify(dataSetObj[x]);
            text+= "--";  //separator
        }
    var result = text.split("--");
    result.pop();
    }

);

You can also export to JSON from your console. Open three dot icon and there will be export and import feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM