使用python解析json数据
我想用python代码解析这个json数据使用python解析json数据,python,python-2.7,Python,Python 2.7,我想用python代码解析这个json数据 { "_class":"com.cloudbees.plugins.credentials.CredentialsStoreAction$DomainWrapper", "credentials":[ { "description":"", "displayName":"guest", "fingerprint":null, "fullName"
{
"_class":"com.cloudbees.plugins.credentials.CredentialsStoreAction$DomainWrapper",
"credentials":[
{
"description":"",
"displayName":"guest",
"fingerprint":null,
"fullName":"system/_/3fad6c4d-3f3a-48e1-9d7c-0f165e105907",
"id":"3fad6c4d-3f3a-48e1-9d7c-0f165e105907",
"typeName":"SSH Username with private key"
},
{
"description":"",
"displayName":"guest",
"fingerprint":null,
"fullName":"system/_/9c078a40-bfce-44bb-96df-9014ac129036",
"id":"9c078a40-bfce-44bb-96df-9014ac129036",
"typeName":"SSH Username with private key"
},
{
"description":"",
"displayName":"guest",
"fingerprint":null,
"fullName":"system/_/199b1b42-62a6-4859-ae94-c6ba832af693",
"id":"199b1b42-62a6-4859-ae94-c6ba832af693",
"typeName":"SSH Username with private key"
},
{
"description":"",
"displayName":"guest",
"fingerprint":null,
"fullName":"system/_/35abdef0-c727-49c9-830a-8acb940a92cc",
"id":"35abdef0-c727-49c9-830a-8acb940a92cc",
"typeName":"SSH Username with private key"
},...
这些数据还在继续。
我从aJSON API获得了这些数据,我想从这里得到显示名、id和typeName的值,但我不确定如何继续
我对python比较陌生,可以想出这段代码
response = requests.get(url, verify=False, auth=(user, api_token))
response.status_code
# print response.text
json_data = response.json()
print json_data
resp_data = json.loads(credentials)
resp_data['displayName']
你很接近。您只需要迭代resp_数据
response = requests.get(url, verify=False, auth=(user, api_token))
response.status_code
json_data = response.json()
resp_data = json_data['credentials']
for credential in resp_data:
print(credential['displayName'], credential['id'], credential['typeName'], sep='---')
应打印出以下响应:
guest---SSH Username with private key---3fad6c4d-3f3a-48e1-9d7c-0f165e105907
guest---SSH Username with private key---9c078a40-bfce-44bb-96df-9014ac129036
guest---SSH Username with private key---199b1b42-62a6-4859-ae94-c6ba832af693
guest---SSH Username with private key---35abdef0-c727-49c9-830a-8acb940a92cc
为了了解有关使用JSON的更多信息,您可以参考以下内容:
还有这个。您需要迭代凭证数组并打印所需的值
response = requests.get(url, verify=False, auth=(user, api_token))
response.status_code
json_data = response.json()
#iterate over credentials array
for credentials in json_data['credentials']:
print(credentials['displayName'])
print(credentials['id'])
print(credentials['typeName'])
可能是打字错误的重复。。。看起来很好,让我们看看,crtl-a选择ctrl-c进行复制,ctrl-v进行粘贴。其中至少有一个是a。。不?