Python JSON Google Translator parsing with Simplejson problem -
i trying parse google translation result using simplejson in python. getting following exception.
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) this json object looks like
{'translations': [{'translatedtext': 'fleur'}, {'translatedtext': 'voiture'}]} could tell me problem here?
you doing simplejson.loads(str(trans_text))
trans_text not string (str or unicode) or buffer object. witnessed simplejson error message, , report of repr(trans_text):
this repr of trans text
{'translations': [{'translatedtext': 'hola'}]}
trans_text dictionary.
if want convert json string, need use simplejson.dumps(), not simplejson.loads().
if want use result else, need dig data out e.g.
# other example trans_text = {'translations': [{'translatedtext': 'fleur'}, {'translatedtext': 'voiture'}]} x in trans_text['translations']: print "chunk of translated text:", x['translatedtext']
Comments
Post a Comment