Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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
将JSON字符串从zmq(C)传输到Python问题_Python_C_Json_Zeromq - Fatal编程技术网

将JSON字符串从zmq(C)传输到Python问题

将JSON字符串从zmq(C)传输到Python问题,python,c,json,zeromq,Python,C,Json,Zeromq,我有一个C程序,它使用Jansson库生成JSON字符串。然后,字符串通过ZMQ套接字发送到Python侦听器,该侦听器正试图使用json库来解码json字符串。我在JSON解码方面遇到了问题,因为我认为引号符号在传输过程中被弄乱了 在C中,我生成以下JSON对象: {“ticker”:“GOOG”} 使用以下代码 strcpy(jsonStrArr,{\'ticker\':\'GOOG\'}\0) 在python中,我使用以下代码打印收到的内容: print'Received'+repr(rx

我有一个C程序,它使用Jansson库生成JSON字符串。然后,字符串通过ZMQ套接字发送到Python侦听器,该侦听器正试图使用json库来解码json字符串。我在JSON解码方面遇到了问题,因为我认为引号符号在传输过程中被弄乱了

在C中,我生成以下JSON对象:

{“ticker”:“GOOG”}

使用以下代码
strcpy(jsonStrArr,{\'ticker\':\'GOOG\'}\0)

在python中,我使用以下代码打印收到的内容:
print'Received'+repr(rxStr)+'on Queue_B'

我看到的打印输出是:

在队列_B上收到“{u'ticker':u'GOOG'}”

我不是JSON专家,但我认为u'会弄乱JSON.loads()函数,因为需要双引号

我知道我需要为jsonStrArr变量添加一些内容,但不确定是什么


提前谢谢。

不,美国没有搞砸任何事情

u'string'表示它是一个unicode字符串

在Python2中运行此命令

# -*- coding: utf-8 -*-

a = '؏' # It's just some arabic character I googled for, definitely not ascii
b = u'؏' 

print type(a)
>>> <type 'str'>
print type(b)
>>> <type 'unicode'>

再一次,Python3在打印字符串时的行为会有所不同,因为在Python3中,如果我没记错的话,字符串自动是unicode字符串。

你说的很有效,所以代码行应该是:
rxStr=json.dumps(rx)
,然后我可以使用unicode格式的键访问字典,比如
rxDict[u'GOOG']
。看起来有点违反直觉,我已经有了一个JSON字符串,我先在它上面做
JSON.dumps
,而不是
JSON.loads
import json

a = json.loads(u'{"ticker": "GOOG"}')

print a
>>> "{u'ticker': u'GOOG'}"
print json.dumps(a)
>>> {"ticker": "GOOG"}