英文:
Golang JSON Unmarshaling of python string
问题
我正在使用AWS SQS队列,其中Python使用boto将字符串放入队列中。大部分字符串都是Unicode,但有些是Python的基本字符串类型。
我正在尝试使用GO读取这些消息,并遇到JSON解码错误:
JSON解码错误:2个无效字符'e',寻找值的开始
所有这些解码错误都发生在GO遇到不是Unicode字符串而是Python基本字符串格式的消息时。
有没有办法从GO将Python字符串转换为Unicode字符串?ascii -> unicode?
编辑:
这是一个工作和不工作的JSON字符串示例。我唯一能够获得不工作的字符串是通过Python,GO会给出上述解码错误
工作的:
u'{"queue_time": "1374523279747", "object_id": "5efc90c0912fef247f028f1758082a299c018e8a2c6499289f3426217b14f0ae", "source_bucket": "ap1-cache"}',
不工作的:
'{"queue_time": "1374523279.75026", "source_bucket": "eu1-cache", "object_id": "42e8b7b01ed5919a23d6d91fdc0b540944a5f45e4c387fa1c2bc20e1bf81bdeb"}',
英文:
I'm working with an AWS SQS queue where python is putting strings into the queue using boto. Most of the strings are unicode, but some are in pythons basic string type.
I'm trying to read these messages with GO and running into JSON decode errors:
JSON Decode Error: 2 invalid character 'e' looking for beginning of value
All these decode errors happen when GO encounters a messages that is not a unicode string but pythons basic string format.
Is there a way of converting python strings into unicode strings from GO? ascii -> unicode?
EDIT:
Here's an example of a working and non working json string. The only way I'm able to get the non working one is through python, GO gives the decode error above
Working:
u'{"queue_time": "1374523279747", "object_id": "5efc90c0912fef247f028f1758082a299c018e8a2c6499289f3426217b14f0ae", "source_bucket": "ap1-cache"}',
Doesn't work:
'{"queue_time": "1374523279.75026", "source_bucket": "eu1-cache", "object_id": "42e8b7b01ed5919a23d6d91fdc0b540944a5f45e4c387fa1c2bc20e1bf81bdeb"}',
答案1
得分: 2
要从Python生成正确的JSON,您可以使用json
库:
>>> d = {"queue_time": "1374523279747", "object_id": "...", "source_bucket": "ap1-cache"}
>>> d
{'queue_time': '1374523279747', 'source_bucket': 'ap1-cache', 'object_id': '...'}
>>> import json
>>> print json.dumps(d)
{"queue_time": "1374523279747", "source_bucket": "ap1-cache", "object_id": "..."}
请注意,对于这个简单的示例,JSON输出看起来几乎完全相似,但对于更复杂的情况,情况可能并非如此。例如,元组和Unicode字符串具有不同的表示方式:
>>> t = [u"Hello", ("World", "!"), u"\xa0"]
>>> t
[u'Hello', ('World', '!'), u'\xa0']
>>> print json.dumps(t)
["Hello", ["World", "!"], "\u00a0"]
英文:
To produce proper JSON from Python, you can use the json
library:
>>> d = {"queue_time": "1374523279747", "object_id": "...", "source_bucket": "ap1-cache"}
>>> d
{'queue_time': '1374523279747', 'source_bucket': 'ap1-cache', 'object_id': '...'}
>>> import json
>>> print json.dumps(d)
{"queue_time": "1374523279747", "source_bucket": "ap1-cache", "object_id": "..."}
Note that the JSON output looks almost entirely similar for this simple example, but that's not necessarily true for more complicated things. E.g. tuples and unicode strings have different representations:
>>> t = [u"Hello", ("World", "!"), u"\xa0"]
>>> t
[u'Hello', ('World', '!'), u'\xa0']
>>> print json.dumps(t)
["Hello", ["World", "!"], "\u00a0"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论