Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Firebase发送的JSON无效_Python_Json_Node.js_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Python Firebase发送的JSON无效

Python Firebase发送的JSON无效,python,json,node.js,firebase,firebase-cloud-messaging,Python,Json,Node.js,Firebase,Firebase Cloud Messaging,这段代码是由Firebase返回的,我得到的错误是:无法解码任何JSON对象。我认为这与JSON格式的有效性有关 我使用Firebase Node.JS SDK获取这些JSON数据。然后我将使用Pyshell将其传递给Python。当我在python中使用json.loads时,tt说: { name: 'anonymous', text: 'Hello' } { name: 'anonymous', text: 'How are you' } { name: 'anonymous', text

这段代码是由Firebase返回的,我得到的错误是:无法解码任何JSON对象。我认为这与JSON格式的有效性有关

我使用Firebase Node.JS SDK获取这些JSON数据。然后我将使用Pyshell将其传递给Python。当我在python中使用
json.loads
时,tt说:

{ name: 'anonymous', text: 'Hello' }
{ name: 'anonymous', text: 'How are you' }
{ name: 'anonymous', text: 'I am fine' }
C:\Python27>节点firebase2.js
{name:'anonymous',text:'Hello'}
{姓名:'匿名',文字:'你好'}
{姓名:'匿名',文字:'我很好'}
C:\Python27\firebase2.js:40
如果(错误)抛出错误;
^
错误:ValueError:无法解码任何JSON对象
在PythonShell.parseError(C:\Python27\node\u modules\PythonShell\index.js:183:17)
终止时需要(C:\Python27\node\u modules\pythonshell\index.js:98:28)
在这个过程中。(C:\Python27\node\u modules\pythonshell\index.js:88:9)
两点钟(events.js:87:13)
在ChildProcess.emit(events.js:172:7)
在Process.ChildProcess.\u handle.onexit(internal/child\u Process.js:200:12)
-----Python回溯-----
文件“my_script.py”,第3行,在
myjson=json.loads(myinput)
文件“C:\Python27\lib\json\\ uuuuu init\uuuuu.py”,第339行,加载
返回\u默认\u解码器。解码
文件“C:\Python27\lib\json\decoder.py”,第364行,在decode中
obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
文件“C:\Python27\lib\json\decoder.py”,第382行,原始解码
raise VALUERROR(“无法解码JSON对象”)

这不是一个有效的JSON,我认为您的
firebase2.js
在这里是错误的

与此相反:

C:\Python27>node firebase2.js
{ name: 'anonymous', text: 'Hello' }
{ name: 'anonymous', text: 'How are you' }
{ name: 'anonymous', text: 'I am fine' }
C:\Python27\firebase2.js:40
          if (err) throw err;
                   ^

Error: ValueError: No JSON object could be decoded
    at PythonShell.parseError (C:\Python27\node_modules\python-shell\index.js:183:17)
    at terminateIfNeeded (C:\Python27\node_modules\python-shell\index.js:98:28)
    at ChildProcess.<anonymous> (C:\Python27\node_modules\python-shell\index.js:88:9)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
    ----- Python Traceback -----
    File "my_script.py", line 3, in <module>
      myjson = json.loads(myinput)
    File "C:\Python27\lib\json\__init__.py", line 339, in loads
      return _default_decoder.decode(s)
    File "C:\Python27\lib\json\decoder.py", line 364, in decode
      obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
      raise ValueError("No JSON object could be decoded")
它应该输出以下内容:

{ name: 'anonymous', text: 'Hello' }
{ name: 'anonymous', text: 'How are you' }
{ name: 'anonymous', text: 'I am fine' }
所有字符串(包括对象键)都必须用双引号引起来。数组必须包含在方括号中,数组元素需要用逗号分隔

查看您的
firebase2.js
程序,看看它是如何生成输出的。如果它使用的不是单个:

[
  { "name": "anonymous", "text": "Hello" },
  { "name": "anonymous", "text": "How are you" },
  { "name": "anonymous", "text": "I am fine" }
]
这就是你的问题

无论如何,我非常肯定Firebase不会回来
{a:'b'}{c:'d'}
而不是
[{a:'b'},{c:'d'}]
——这是不懂JSON格式的初学者的典型错误,对于世界上最大的API提供商之一来说,这是很难相信的

如果您想知道真正的响应是什么,请使用
curl

console.log(JSON.stringify(SOME_VARIABLE));
如果您在那里看到无效的JSON,那么是时候联系Firebase支持人员了


JSON格式在上进行了解释-这是现有的最简单的数据格式。

经过一些调试后,我发现了错误

curl -v https://example.com/some/endpoint -H 'auth header' ...
以下行删除了错误:

ref.on("child_added", function(snapshot, prevChildKey) {
  var newPost = snapshot.val();
  newPost = JSON.stringify(newPost);      //this line corrected my error
  });


顺便说一句,谢谢大家指导我。

有什么问题吗?是的,这是无效的JSON,如果它是JSON,至少有四种不同的方式:1。没有单个根值(例如,对象或数组)。2.对象之间没有逗号。3.键不在双引号中。4.字符串不是双引号。@t.J.Crowder,但此数据不是我生成的。它由firebase实时数据库生成。如何在每次获取数据时以编程方式对其进行格式化,以便进一步处理?@AakashBansal这是来自FCM服务器的响应吗?还是另一个API调用?@AL,这是直接来自服务器的响应
newPost = JSON.stringify(newPost);