Answer the question
In order to leave comments, you need to log in
python json
The question is, I get json from google translate and it does not want to be parsed.
What am I doing wrong?
produces the following
res = urllib2.urlopen(request).read()
data = json.loads(res)
Traceback (most recent call last):
File "./translate.py", line 27, in data = json.loads(res)
File "/usr/lib/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/usr/lib/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib/python2.6/json/decoder.py", line 219, in JSONArray
raise ValueError(errmsg("Expecting object", s, end))
ValueError: Expecting object: line 1 column 158 (char 158)
если сделать print(res) то выводит:
[,,["interjection",["привет","эй","ну","салют","здор `ово"]]],"en",,,,,,,21]
Answer the question
In order to leave comments, you need to log in
Threw a small fix. The point is empty,,, it was
<?php
$s = '[,,["глагол",["здороваться","звать","окликать"]],["междометие",["привет","здравствуйте","алло"]]],"en",,,,,,,16]';
$s = preg_replace('`,{2,}`', ',', $s);
$s = str_replace(',]', ']', $s);
print_r(json_decode($s));
This is not valid JSON, but quite valid JavaScript code, Google uses just the second option, so everything works for it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question