如何将 firebase 响应存储在 Json 数组中

How to store firebase response in a Json Array

目前我有一组硬编码的 url 以接收 html 中的图像。

    $scope.product = {
    "images" : [
    {"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"},
    {"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"},
    {"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"}

  ] };

我刚刚学会了如何从数据库中接收数据,但如何将结果存储在 json 数组 中,就像“$scope.product"

我目前正在通过

接收数据
// Get a reference to our posts
var ref = new Firebase("https://<database>.firebaseio.com/profile");
// Retrieve new posts as they are added to our database
ref.on("child_added", function(snapshot, prevChildKey) {
  var newPost = snapshot.val();
  debugger;
  console.log("The email is: " + newPost.email);
}); 

请记住,javascript 数组和对象不是 JSON 数组和字典,尽管它们非常匹配。您似乎希望在收到数据时根据数据创建一个 javascript 对象。

鉴于您的代码,将其调整为类似这样的结构应该会产生类似于您所询问的结构:

// Get a reference to our posts
var ref = new Firebase("https://<database>.firebaseio.com/profile");
// some place to store stuff
var results = {
  'emails':[]
};
// Retrieve new posts as they are added to our database
ref.on("child_added", function(snapshot, prevChildKey) {
  var newPost = snapshot.val();
  results.emails.push({
    'email':newPost.email
  });
});

将 javascript results 对象转换为 JSON 字符串非常简单:

var json_string = JSON.stringify(results);

有几种排列数据的方法。我只能提供已知元素的代码。