如何在python中解析JSON对象数组

时间:2018-01-10 14:32:16

标签: python json

我从 open c for 'select * from ' || i.owner || '.<table_name>'; 请求的JSON响应中收到了以下POST数组:

HTTP

鉴于[{ "username": "username_1", "first_name": "", "last_name": "", "roles": "system_admin system_user", "locale": "en", "delete_at": 0, "update_at": 1511335509393, "create_at": 1511335500662, "auth_service": "", "email": "userid_1@provider_1.com", "auth_data": "", "position": "", "nickname": "", "id": "short-string-of-random-characters-1" }, { ... } <more such objects>..] 给了我typeof(response),我如何在requests.models.Response解析它?

4 个答案:

答案 0 :(得分:2)

您可以尝试以下方式从json响应中获取值:

import json

content=[{
    "username": "admin",
    "first_name": "",
    "last_name": "",
    "roles": "system_admin system_user",
    "locale": "en",
    "delete_at": 0,
    "update_at": 1511335509393,
    "create_at": 1511335500662,
    "auth_service": "",
    "email": "adminuser@cognizant.com",
    "auth_data": "",
    "position": "",
    "nickname": "",
    "id": "pbjds5wmsp8cxr993nmc6ozodh"
}, {
    "username": "chatops",
    "first_name": "",
    "last_name": "",
    "roles": "system_user",
    "locale": "en",
    "delete_at": 0,
    "update_at": 1511335743479,
    "create_at": 1511335743393,
    "auth_service": "",
    "email": "chatops@cognizant.com",
    "auth_data": "",
    "position": "",
    "nickname": "",
    "id": "akxdddp5p7fjirxq7whhntq1nr"
}]

for item in content:
    print("Name: {}\nEmail: {}\nID: {}\n".format(item['username'],item['email'],item['id']))

输出:

Name: admin
Email: adminuser@cognizant.com
ID: pbjds5wmsp8cxr993nmc6ozodh

Name: chatops
Email: chatops@cognizant.com
ID: akxdddp5p7fjirxq7whhntq1nr

答案 1 :(得分:0)

看看json module。更具体地说,解码JSON:&#39;部分。

import json
import requests

response = requests.get()  # api call

users = json.loads(response.text)
for user in users:
    print user['id']

答案 2 :(得分:-1)

您正在寻找的是json模块。使用它,您可以使用它将字符串解析为json格式:

git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true

答案 3 :(得分:-1)

使用python 3并导入urlib

import urllib.request
import json
url = link of the server 
#Taking response and request  from url

r = urllib.request.urlopen(url)
#reading and decoding the data
data = json.loads(r.read().decode(r.info().get_param('charset') or 'utf-8'))


for json_inner_array in data:
        for json_data in json_inner_array:
                    print("id: "+json_data["id"])