我有这个JSON数据
{
"Label": "Color Roles",
"Pool": "Colors",
"Hidden": false,
"Limit": -3,
"Message": "Pick a colored role to start with <3",
"Roles": [
{
"Name": "Apple★",
"Print": "Apple",
"Aliases": ".iam Apple",
"Aliases1": [
".iam Apple",
"+Apple"
]
},
{
"Name": "Clementine★",
"Print": "Clementine",
"Aliases": [
".iam Clementine",
"+Clementine"
]
},
{
"Name": "Sunflower★",
"Print": "Sunflower",
"Aliases": [
".iam Sunflower",
"+Sunflower"
]
},
{
"Name": "Mint★",
"Print": "Mint",
"Aliases": [
".iam Mint",
"+Mint"
]
},
{
"Name": "Ocean★",
"Print": "Ocean",
"Aliases": [
".iam Ocean",
"+Ocean"
]
},
{
"Name": "Grape★",
"Print": "Grape",
"Aliases": [
".iam Grape",
"+Grape"
]
},
{
"Name": "Candy Floss★",
"Print": "Candy Floss",
"Aliases": [
".iam Candy Floss",
"+Candy Floss"
]
}
]
}
]我想将"Roles"中的所有"Roles"格式化为一个数组,然后打印。我只能使用代码打印第一个“名称”
print(data['roles'][0]['name'])我怎样才能把它格式化以便打印出来
["Apple★","Clementine★","Sunflower★","Mint★","Ocean★","Grape★","Candy Floss★"]然后将该数据保存在数组中,以便用于另一个函数,该函数将充当
values = ["Apple★","Clementine★","Sunflower★","Mint★","Ocean★","Grape★","Candy Floss★"]
info = requests.get(https://api.url.com/{Values?}只需对循环中的每个值执行请求。
发布于 2022-06-27 05:54:57
您可以使用对Roles数据集的列表理解来获取Name属性:
values = [role['Name'] for role in data['Roles']]输出(用于样本数据):
["Apple★", "Clementine★", "Sunflower★", "Mint★", "Ocean★", "Grape★", "Candy Floss★"]发布于 2022-06-27 06:39:56
jsonpath 包专门用于处理json数据.。
你只需要学习一些简单的语法就可以操作了。
from jsonpath import jsonpath
data = {
"Label": "Color Roles",
"Pool": "Colors",
"Hidden": "false",
"Limit": -3,
"Message": "Pick a colored role to start with <3",
"Roles": [
{
"Name": "Apple★",
"Print": "Apple",
"Aliases": ".iam Apple",
"Aliases1": [
".iam Apple",
"+Apple"
]
},
{
"Name": "Clementine★",
"Print": "Clementine",
"Aliases": [
".iam Clementine",
"+Clementine"
]
},
{
"Name": "Sunflower★",
"Print": "Sunflower",
"Aliases": [
".iam Sunflower",
"+Sunflower"
]
},
{
"Name": "Mint★",
"Print": "Mint",
"Aliases": [
".iam Mint",
"+Mint"
]
},
{
"Name": "Ocean★",
"Print": "Ocean",
"Aliases": [
".iam Ocean",
"+Ocean"
]
},
{
"Name": "Grape★",
"Print": "Grape",
"Aliases": [
".iam Grape",
"+Grape"
]
},
{
"Name": "Candy Floss★",
"Print": "Candy Floss",
"Aliases": [
".iam Candy Floss",
"+Candy Floss"
]
}
]
}
print(jsonpath(data, "$..Name"))输出
['Apple★', 'Clementine★', 'Sunflower★', 'Mint★', 'Ocean★', 'Grape★', 'Candy Floss★']https://stackoverflow.com/questions/72767443
复制相似问题