Firebase使用浮点数作为键 [英] Firebase Using Floating-Point Number as Key

查看:134
本文介绍了Firebase使用浮点数作为键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到,当将一些JSON数据推送到我的Firebase存储(其密钥为浮点数)时,服务器会收到400个HTTP错误请求。这是我得到的回应:

$ p $ {error:无效的数据;无法解析JSON对象,数组或

我发送的数据是如下:

pre $ code>'{{36.5:4050952597550},{41.5:4050952597628}]

我相信这完全是Python中一个有效的JSON字符串,因为在编码/解码时没有错误。
$ b $ pre $ import json
v = [{u'36.5':u'4050952597550'},{u'41.5' :u'4050952597628'}]
print v == json.loads(json.dumps(v))
True

这是某种错误还是我错过了某些东西?

解决方案

这是有效的JSON,但它不是有效的Firebase。它似乎并不喜欢这些时期。如果你真的必须使用浮动的属性名称(这听起来有问题),你可以尝试用其他字符,如下划线或逗号替换句点。



取自在Firebase文档中创建参考页面:


$ b请注意,用于构建Firebase引用的网址可能包含任何Unicode字符,除了以下内容:$ b

字符集限制



b
$ b


  • 。 (期间)
    $(美元符号)
  • [(左方括号)

  • ](右方括号)
  • #(散列或英镑符号)
  • /(正斜杠)


    和ASCII控制字符0-31和127。

    你可以用这个正则表达式检查这些字符是否存在: / p>

      / [\ [\]。#$ \ / \\\-\\001F\\\] / 


    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. 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.

    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. 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:

    Character Set Limitations

    Note that URLs used to construct Firebase references may contain any unicode characters except:

    • . (period)
    • $ (dollar sign)
    • [ (left square bracket)
    • ] (right square bracket)
    • # (hash or pound sign)
    • / (forward slash)

    and ASCII control characters 0-31 and 127.

    You could check for the existence of these characters with this regular expression:

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

    这篇关于Firebase使用浮点数作为键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆