从Json Python获取特定字段值

从Json Python获取特定字段值,python,arrays,json,list,dictionary,Python,Arrays,Json,List,Dictionary,我有JSON作为响应,我正在尝试获取所有“Id”和“Pages”值,并将它们放入一个数组(或列表)以供下一步使用 [ { "Page": 1, "Content": [ {"Id": 100000000000001,"Title": "title1", ...}, {"Id": 100000000000002,"Title": "title2", ...}, {"Id": 100000000000003,"Title": "titl

我有JSON作为响应,我正在尝试获取所有“Id”和“Pages”值,并将它们放入一个数组(或列表)以供下一步使用

[
{
    "Page": 1,
    "Content": [
        {"Id": 100000000000001,"Title": "title1", ...},
        {"Id": 100000000000002,"Title": "title2", ...},
        {"Id": 100000000000003,"Title": "title3", ...}
    ]
},
{
    "Page": 2,
    "Content": [
        {"Id": 100000000000004,"Title": "title4", ...},
        {"Id": 100000000000005,"Title": "title5", ...},
        {"Id": 100000000000006,"Title": "title6", ...}
    ]
},
{
    "Page": 3,
    "Content": [
        {"Id": 100000000000007,"Title": "title7", ...},
        {"Id": 100000000000008,"Title": "title8", ...},
        {"Id": 100000000000009,"Title": "title9", ...}
    ]
}
]

通过使用
pages=[e['Page']获取“Page”值,表示数据中的e]

无法获取“Id”值。尝试

对于el in数据:
打印(el['Content']['Id'])

但是得到了错误
TypeError:列表索引必须是整数或片,而不是str

你能帮我吗


更新1:很抱歉我的问题有点不正确:作为这个JSON的输出,我想返回数组[“id1”、“id2”、“id9”],而不是打印您的
'Content'
字段是一个
列表,可能有多个元素本身包含一个
'Id'
字段。如果你想把它们全部打印出来,这里有一种方法

for element in data:
    ids = [c['id'] for c in element['Content']]
    print(ids)

您的
'Content'
字段是一个
列表
,可能有多个元素本身包含一个
'Id'
字段。如果你想把它们全部打印出来,这里有一种方法

for element in data:
    ids = [c['id'] for c in element['Content']]
    print(ids)

使用列表理解,您可以轻松做到这一点

a = [
{
    "Page": 1,
    "Content": [
        {"Id": 100000000000001,"Title": "title1",  },
        {"Id": 100000000000002,"Title": "title2",  },
        {"Id": 100000000000003,"Title": "title3",  }
    ]
},
{
    "Page": 2,
    "Content": [
        {"Id": 100000000000004,"Title": "title4",  },
        {"Id": 100000000000005,"Title": "title5",  },
        {"Id": 100000000000006,"Title": "title6",  }
    ]
},
{
    "Page": 3,
    "Content": [
        {"Id": 100000000000007,"Title": "title7",  },
        {"Id": 100000000000008,"Title": "title8",  },
        {"Id": 100000000000009,"Title": "title9",  }
    ]
}

]

res = [[i['Page'],[ j['Id'] for j in i['Content']]] for i in a]
print(res)
输出

[[1, [100000000000001, 100000000000002, 100000000000003]],
 [2, [100000000000004, 100000000000005, 100000000000006]],
 [3, [100000000000007, 100000000000008, 100000000000009]]]

使用列表理解,您可以轻松做到这一点

a = [
{
    "Page": 1,
    "Content": [
        {"Id": 100000000000001,"Title": "title1",  },
        {"Id": 100000000000002,"Title": "title2",  },
        {"Id": 100000000000003,"Title": "title3",  }
    ]
},
{
    "Page": 2,
    "Content": [
        {"Id": 100000000000004,"Title": "title4",  },
        {"Id": 100000000000005,"Title": "title5",  },
        {"Id": 100000000000006,"Title": "title6",  }
    ]
},
{
    "Page": 3,
    "Content": [
        {"Id": 100000000000007,"Title": "title7",  },
        {"Id": 100000000000008,"Title": "title8",  },
        {"Id": 100000000000009,"Title": "title9",  }
    ]
}

]

res = [[i['Page'],[ j['Id'] for j in i['Content']]] for i in a]
print(res)
输出

[[1, [100000000000001, 100000000000002, 100000000000003]],
 [2, [100000000000004, 100000000000005, 100000000000006]],
 [3, [100000000000007, 100000000000008, 100000000000009]]]

这只是对已经给出的答案的补充,以防您遇到更多嵌套路径:使您可以轻松遍历嵌套列表和目录:

页面路径为数据->列表->页面。。。所以你访问数据,接下来是列表,然后是页面键

同样适用于id:数据->列表->内容->列表->id

在jmespath中,通过点访问键,而通过括号([])访问列表:


jmespath允许u编译搜索路径,类似于python的
re
模块中的功能。当你遇到真正的嵌套路径时,它更像是一个方便的工具。

这只是对已经给出的答案的一个补充,以防你遇到更多嵌套路径:可以轻松地遍历嵌套列表和目录:

页面路径为数据->列表->页面。。。所以你访问数据,接下来是列表,然后是页面键

同样适用于id:数据->列表->内容->列表->id

在jmespath中,通过点访问键,而通过括号([])访问列表:


jmespath允许u编译搜索路径,类似于python的
re
模块中的功能。当您遇到真正嵌套的路径时,它更像是一个方便的工具。

谢谢您的回答。如果你不介意的话,还有两个问题:1。如何将“id”放入数组(不想立即打印)?因为现在它打印数组,数组在2内。如何将具有数组[]、[]、[]的数组转换为具有所有值的一个数组?作为一般输出,我想得到数组[“id1”、“id2”、“id3”、…、“id9”]
flatten=[element for subarray in array for element in subarray]
感谢您的回答。如果你不介意的话,还有两个问题:1。如何将“id”放入数组(不想立即打印)?因为现在它打印数组,数组在2内。如何将具有数组[]、[]、[]的数组转换为具有所有值的一个数组?作为一般输出,我想得到数组[“id1”、“id2”、“id3”、…、“id9”]
flatten=[element for subarray in array for element in subarray]
感谢您的回答。很抱歉,我的问题有点不正确:作为JSON的输出,我想返回数组[“id1”、“id2”、…、“id9”],但不要打印这些“ID”符号作为答案。很抱歉,我的问题有点不正确:作为JSON的输出,我想返回数组[“id1”、“id2”、…、“id9”],但不打印这些“ID”