python, json

0 like 0 dislike
8 views
The question is, get json from google translate and he doesn't want to parse out.
What are you doing wrong?
res = urllib2.urlopen(request).read()
data = json.loads(res)


produces the following
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 (158 char)

if you do print(res) that displays:
[[["Privet","hi","privet","]],[["abbreviation",["Hawaii"]],["interjection",["Privet","a","no","Salut",".`ouo"]]],"en",,[["Privet",[5],1,,1000,0,1,0]],[["hi",4,,,"],["hi",5,[["Privet",1000,1,]],[[0,2]],"hi"]],,,,21]
by | 8 views

3 Answers

0 like 0 dislike
Distribute a small fix. The thing is empty, it
\r
\r<?php\r$s = '[[["привет","hello","privet",""]],[["существительное",["приветствие","приветственный возглас","возглас удивления"]],["глагол",["здороваться","звать","окликать"]],["междометие",["привет","здравствуйте","алло"]]],"en",,[["привет",[5],1,,859,0,1,0]],[["hello",4,,,""],["hello",5,[["привет",859,1,],["Привете",0,1,],["Приветом",0,1,],["Привету",0,1,],["Привета",0,1,]],[[0,5]],"hello"]],,,,16]';\r\r$s = preg_replace('`,{2,}`', ',', $s);\r$s = str_replace(',]', ']', $s);\r\rprint_r(json_decode($s));\r</code>
by
0 like 0 dislike
This is not valid JSON, but it is valid JavaScript code, Google uses exactly the second option, so it all works.
by
0 like 0 dislike
something with json, not parse Pochepa too
by

Related questions

0 like 0 dislike
1 answer
asked Jun 1, 2019 by AleksandrB
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
32,905 users