简体   繁体   English

为 Firebase 操作 JSON 文件

[英]Manipulating a JSON file for Firebase

I am trying to upload a JSON file to Firebase using python but keep getting the error message: "Key value can't be empty or contain $ # [ ] / or."我正在尝试使用 python 将 JSON 文件上传到 Firebase,但不断收到错误消息:“键值不能为空或包含 $ # [ ] / 或。”

My JSON file(extract bellow) does contain a full stop in each key so I assume that is the problem.我的 JSON 文件(下面的摘录)在每个键中确实包含一个句号,所以我认为这就是问题所在。 My question is if there is a way I could manipulate the JSON file and remove the number followed by the full stop programmatically for all the keys for which there are hundreds of?我的问题是,是否有一种方法可以操纵 JSON 文件,并以编程方式为所有有数百个键的句号删除数字?

"2021-09-24 20:00:00": {
    "1. open": "146.9300",
    "2. high": "147.0100",
    "3. low": "146.9300",
    "4. close": "146.9700",
    "5. volume": "28859"
}

I'll do it this way.我会这样做的。 Not sure if It's the best, but It works;-)不确定它是否是最好的,但它有效;-)

from json import loads

json_data = \
"""
{
"2021-09-24 20:00:00": {
    "1. open": "146.9300",
    "2. high": "147.0100",
    "3. low": "146.9300",
    "4. close": "146.9700",
    "5. volume": "28859"
}
}
"""
data = dict(loads(json_data))

for date in data:
    for field in data[date]:
        valueString = data[date][field]
        intValue = valueString.split('.')[0]
        data[date][field] = intValue

print(data)

Output: Output:

{"2021-09-24 20:00:00": {"1. open": "146", "2. high": "147", "3. low": "146", "4. close": "146", "5. volume": "28859"}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 JSON 文件上传到 firebase - How to upload JSON file to firebase 在没有 Firebase JSON 文件的情况下验证 Firestore? - Authenticate Firestore without Firebase JSON File? Firebase 函数保存 JSON 到本地文件 - Firebase functions saving JSON to local file 无法从 Firebase 控制台下载 google-services.json 文件 - Unable to download google-services.json file from Firebase Console HTTP 请求 - 使用 Axios 在 Firebase 中删除 JSON 文件密钥 - HTTP Request - Delete JSON file key in Firebase with Axios 如何使用 Firebase 函数在 Firebase 存储上上传一个 JSON 的新文件? - How do you upload a new file with a JSON on Firebase Storage using Firebase Functions? 解析错误:遵循 Firebase Cloud Functions 初始化说明后无法读取文件 '\tsconfig.json' eslint - Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions 在最新的 ZC31B32364CE19CA8FCD150A417ECCE5 中将 firebase json 文件添加到 gradle 项目级别面临困难 - facing difficulty to add firebase json file to gradle project level in latest android version 每次在 firebase 中添加 sha 密钥时,是否需要下载谷歌服务 json 文件? - Do I need to download google service json file everytime I add sha key in firebase? 寻找一种使用 firebase 在无服务器 flutter 中存储和读取 json 文件的安全方法 - Looking for a secure way to store and read json file in serverless flutter using firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM