简体   繁体   English

使用浮点数作为键的Firebase

[英]Firebase Using Floating-Point Number as Key

I realized that I get 400 HTTP Bad Request from the server when pushing some JSON data into my Firebase storage whose keys are floating-point numbers. 我意识到,当将一些JSON数据推送到我的Firebase存储时,我从服务器得到400 HTTP错误请求,其密钥是浮点数。 Here is the response I got: 这是我得到的回应:

    {"error" : "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."}

The data I sent is as follows: 我发送的数据如下:

'[{"36.5": "4050952597550"}, {"41.5": "4050952597628"}]'

I believe it is perfectly a valid JSON string in Python because I get no errors while encoding/decoding it. 我相信它在Python中完全是有效的JSON字符串,因为在对其进行编码/解码时没有错误。

import json
v = [{u'36.5': u'4050952597550'}, {u'41.5': u'4050952597628'}]
print v == json.loads(json.dumps(v))
True

Is this some kind of a bug or am I missing something? 这是某种错误还是我错过了一些东西?

It's valid JSON, but it's not valid Firebase. 它是有效的JSON,但不是有效的Firebase。 It doesn't appear to like the periods. 它似乎不喜欢这些时期。 If you really have to use floats for your property names (which sounds questionable), you can try replacing the periods with other characters, like underscores or commas. 如果你真的必须使用浮点数作为你的属性名称(这听起来有问题),你可以尝试用其他字符替换句点,如下划线或逗号。

Taken from the Creating References page in Firebase's documentation: 取自Firebase文档中的“ 创建参考”页面:

Character Set Limitations 字符集限制

Note that URLs used to construct Firebase references may contain any unicode characters except: 请注意,用于构造Firebase引用的URL可能包含任何Unicode字符,但以下情况除外:

  • . (period) (期)
  • $ (dollar sign) $(美元符号)
  • [ (left square bracket) [(左方括号)
  • ] (right square bracket) ](右方括号)
  • # (hash or pound sign) #(井号或井号)
  • / (forward slash) /(正斜杠)

and ASCII control characters 0-31 and 127. 和ASCII控制字符0-31和127。

You could check for the existence of these characters with this regular expression: 您可以使用以下正则表达式检查这些字符是否存在:

/[\[\].#$\/\u0000-\u001F\u007F]/

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

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