【发布时间】:2014-05-16 22:27:51
【问题描述】:
作为API call 的结果,我得到<type 'unicode'> 的以下对象:
{"From":"en","Translations":[{"Count":0,"MatchDegree":100,"MatchedOriginalText":"","Rating":5,"TranslatedText":"Cómo estás"}]}
但是当我尝试用simplejson_loads() 解析它时,我得到了这个错误:
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我该如何处理这种对象?
EDIT II:JSON 是正确的。让事情变得混乱的是字符串开头的 BOM。试图用.encode('utf-8-sig') 摆脱它会产生错误
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 2: ordinal not in range(128)
但在 this discussion 的某个地方,我找到了适合我的解决方案:
if u.startswith(u'\ufeff'):
u = u[1:]
我很想摆脱它并快乐起来。
【问题讨论】:
-
无法复制。
import json; json.loads(u'that string')给了我预期的dict。 -
@roippi 你是对的。它看起来像一个编码问题。我用更多信息编辑了这个问题......
标签: python unicode python-unicode