解析unicode JSON字符串

时间:2015-03-07 15:58:09

标签: json python-2.7 unicode

我在解析unicode JSON字符串时遇到了一些困难。

样品:

js = "{'to': 1234, 'message': u'sample message', 'user': 65773722, 'msgId': 28198}"

我想迭代JSON对象以检索值。

我已经尝试过,json.dumps,json.loads和js.decode('unicode-escape'), 但我一直收到错误信息。

请帮忙......我被困了!

非常感谢!

2 个答案:

答案 0 :(得分:3)

不幸的是有人傻了,而且那不是JSON。

>>> ast.literal_eval(js)
{'to': 1234, 'message': u'sample message', 'user': 65773722, 'msgId': 28198}

答案 1 :(得分:1)

它无效json

s = "{'to': 1234, 'message': u'sample message', 'user': 65773722, 'msgId': 28198}"
valid = s.replace("u'", "'")
supervalid = v.replace("'", '"')
json.loads(super_valid)