如何从多个数组的json文件中提取数据?

时间:2017-11-16 17:58:28

标签: python json

我正在写一个小瓶子应用程序,它访问json-API。 然后应提取json文件中的所有URL并将其放入列表以供进一步使用。有问题的代码:

@app.route('/booro/<tags>') # <tags> acts as search
def process(tags):
    r=requests.get('https://example.com/post/index.json?'+tags+'&limit=1') #gives me a json file with only one array
    z = r.text[1:-1] # to remove "[" and "]" so that it can be loaded
    i = json.loads(z)
    m = i['file_url']
    return m

上面的代码然后按预期显示了生成页面上的链接。

但如果我将&limit设置为2或更高,我会收到此错误消息。

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user/.local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.local/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/user/Schreibtisch/FlaskProxy/maindrive.py", line 19, in process
    i = json.loads(z)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 1278 (char 1277)

问题是下载的json文件包含多个无法解析的数组。它看起来像这样:

{"id":1383185,"tags":"new main default","file_url": "example1.com"} {"id":1383185, "tags":"vivid original alternative","file_url": "example2.com"}

有没有办法在多个数组中使用相同的键提取值?

1 个答案:

答案 0 :(得分:1)

您不应该删除[],因为返回的对象是JSON数组,将limit设置为1会返回一个数组只有一个对象。您需要不加修改地解析响应内容,即

i = json.loads(r.text)

如果是limit==1,您将拥有:

[{'file_url': 'example1.com', ...}]

如果是limit==2,您将拥有:

[{'file_url': 'example1.com', ...}, {'file_url': 'example2.com', ...}]

等。 通过删除[]json.loads尝试将响应视为单个JSON对象,而不是JSON对象数组,即尝试解析类似的内容:

{"file_url": "example1.com", ...}, {"file_url": "example2.com", ...}

这不是有效的JSON

话虽如此,你必须做这样的事情:

r = requests.get(...)
response_content = json.loads(r.text)
for obj in response_content:
    file_url = obj['file_url']
    # Do something with your file_url here. Ex. append to list m.append(file_url)