1

我想将数据从 json 推送到 firebase,而不为每个对象生成新密钥。理想情况下,我想定义密钥的名称。

这是我的代码:

import pyrebase
import json

config = {my firebase config}
firebase = pyrebase.initialize_app(config)
db = firebase.database()

with open('myjson.json') as response:
    data = json.load(response)

db.child("customer").push(data)

我的 json 看起来像这样:

{
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
}

但实际上我的代码在 firebase 上给了我这个:

"customer": {
 "-Lm_M6OM7MCV3_PFvcSH(random_id_given_by_firebase)": {
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
 }
}

我期望的是:

"customer": {
  "first_customer": {
    "createdat": "2019-08-09T06:40:10+0000",
    "expires": 1572039000.0,
  },
  "second_customer": {
    "createdat": "2019-08-09T06:33:39+0000",
    "expires": 1570048200.0,
  }
}

4

1 回答 1

0

要控制完整路径,请使用set. 所以像:

db.child("customer").child("keyforcustomer").set(data)
于 2019-08-18T16:03:01.187 回答