Python JSON Google Translator解析为Simplejson问题

| 我正在尝试在Python中使用simplejson解析Google翻译结果。但是我收到以下异常。
Traceback (most recent call last):
  File \"Translator.py\", line 45, in <module>
    main()
  File \"Translator.py\", line 41, in main
    parse_json(trans_text)
  File \"Translator.py\", line 29, in parse_json
    json = simplejson.loads(str(trans_text))
  File \"/usr/local/lib/python2.6/dist-packages/simplejson-2.1.3-py2.6-linux-i686.egg/simplejson/__init__.py\", line 385, in loads
    return _default_decoder.decode(s)
  File \"/usr/local/lib/python2.6/dist-packages/simplejson-2.1.3-py2.6-linux-i686.egg/simplejson/decoder.py\", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File \"/usr/local/lib/python2.6/dist-packages/simplejson-2.1.3-py2.6-linux-i686.egg/simplejson/decoder.py\", line 418, in raw_decode
    obj, end = self.scan_once(s, idx)
simplejson.decoder.JSONDecodeError: Expecting property name: line 1 column 1 (char 1)
这是我的json对象看起来像
{\'translations\': [{\'translatedText\': \'fleur\'}, {\'translatedText\': \'voiture\'}]}
谁能告诉我这是什么问题?     
已邀请:
你在做
simplejson.loads(str(trans_text))
“ 3”不是字符串(str或unicode)或缓冲区对象。
simplejson
错误消息和your5ѭ报告证明了这一点:   这是我的跨文本代表   
{\'translations\': [{\'translatedText\':
  \'hola\'}]}
trans_text
是字典。 如果要将其转换为JSON字符串,则需要使用
simplejson.dumps()
,而不是
simplejson.loads()
。 如果您想将结果用于其他用途,则只需将数据挖掘出来,例如
# Your other example
trans_text = {\'translations\': [{\'translatedText\': \'fleur\'}, {\'translatedText\': \'voiture\'}]} 
for x in trans_text[\'translations\']:
    print \"chunk of translated text:\", x[\'translatedText\']
    
问题在于simplejson支持使用双引号编码的字符串而不是单引号编码的字符串的json,因此,天真的解决方案可能是
json.loads(jsonstring.replace(\"\'\", \'\"\'))
    
JSON语法不支持JavaScript的完整语法。与JavaScript不同,JSON字符串和属性名称必须用双引号引起来。   字符串:: =
\"\"
|
\"
字符s13ѭ     

要回复问题请先登录注册