python - 从响应 python 中获取值
问题描述
我正在使用 python 请求向 URL 发出请求并获得以下响应
{
"content": {
"root": {
"details":"http://localhost:8080/****/root",
"content": {
"A": {
"details":"http://localhost:8080/***"
},
"B":{
"details":"http://localhost:8080/***"
},
"C":{
"details":"http://localhost:8080/****"
}
}
}
}
}
我想在列表中获取值 A、B、C。我怎样才能做到这一点?任何输入都会有很大帮助。
解决方案
获取值并将其存储在列表中:
response = {
"content":{
"root":{
"details":"http://localhost:8080/****/root",
"content":{
"A":{
"details":"http://localhost:8080/***"
},
"B":{
"details":"http://localhost:8080/***"
},
"C":{
"details":"http://localhost:8080/***"
}
}
}
}
}
contents = response["content"]['root']['content']
contents_list = []
for content in contents.values():
contents_list.append(content["details"])
print(contents_list)
输出:
['http://localhost:8080/***', 'http://localhost:8080/***', 'http://localhost:8080/***']
但是,如果您想同时获取 A、B 和 C 的键和值。试试这个:
for content in contents.values():
for key, value in content.items():
contents_list.append(f"{key}: {value}")
print(contents_list)
输出:
['details: http://localhost:8080/***', 'details: http://localhost:8080/***', 'details: http://localhost:8080/***']
推荐阅读
- php - .ebextensions 不更新 php.ini 配置
- mysql - 如何查找mysql表记录之间的时间间隔和间隔长度?
- javascript - 如何覆盖组件本身是道具的组件道具
- c# - ASP.Net (C#) VPN 连接
- emacs - 在 Emacs 工具提示中呈现 HTML?
- android - Flutter - oauth2 - Android - redirecturi 不工作
- python - 在 Python 中,如何获取表示为 int 的浮点数的前 N 个十进制数字?
- vue.js - 如何将 Nuxt ssr 部署到 Cpanel
- c# - 在 Linux 中以编程方式写入和读取环境变量
- html - 用于在 html 中显示(嵌入)图像的语法,图像路径是动态的并存储在 DB 字段中