在特定条件下使用Python解析/提取嵌套JSON数据

在特定条件下使用Python解析/提取嵌套JSON数据,python,json,python-3.x,python-2.7,python-requests,Python,Json,Python 3.x,Python 2.7,Python Requests,我正试图从我发出post请求的JSON文件中的细节中提取/解析值 这是JSON文件。我试图从键“AN”获取值。我希望能够提取诸如“shannoncampbell_znyq1”、“katiekapprelmac”等值,以便 从第二行开始,不等于数字零。例如,由于katiekaprelmac的第二行值(该行的值为T7)不等于零,因此我的代码应该将其输出(katiekaprelmac)。但事实并非如此 JSON文件: { "id": "jsonrpc", "jsonrpc": "2.0", "resu

我正试图从我发出post请求的JSON文件中的细节中提取/解析值

这是JSON文件。我试图从键“AN”获取值。我希望能够提取诸如“shannoncampbell_znyq1”、“katiekapprelmac”等值,以便 从第二行开始,等于数字零。例如,由于katiekaprelmac的第二行值(该行的值为T7)不等于零,因此我的代码应该将其输出(katiekaprelmac)。但事实并非如此

JSON文件:

{
"id": "jsonrpc",
"jsonrpc": "2.0",
"result": {
    "result": [
        {
            "AccountId": 697429,
            "Flags": [
                "AutoDeployed"
            ],
            "PartnerId": 287562,
            "Settings": [
                {
                    "AN": "shannoncampbell_znyq1"
                },
                {
                    "T7": "0"
                }
            ]
        },
        {
            "AccountId": 725177,
            "Flags": null,
            "PartnerId": 287562,
            "Settings": [
                {
                    "AN": "katiekapprelmac"
                },
                {
                    "T7": "5"
                }
            ]
        },
        {
            "AccountId": 689130,
            "Flags": [
                "AutoDeployed"
            ],
            "PartnerId": 287562,
            "Settings": [
                {
                    "AN": "sara-pc_wpv7h"
                },
                {
                    "T7": "0"
                }
            ]
        },
        {
            "AccountId": 697531,
            "Flags": null,
            "PartnerId": 287562,
            "Settings": [
                {
                    "AN": "kaelaweeksmac"
                },
                {
                    "T7": "0"
                }
            ]
        },
        {
            "AccountId": 615877,
            "Flags": null,
            "PartnerId": 249098,
            "Settings": [
                {
                    "AN": "elenimacbookpro"
                },
                {
                    "T7": "0"
                }
            ]
        },
        {
            "AccountId": 700661,
            "Flags": null,
            "PartnerId": 287562,
            "Settings": [
                {
                    "AN": "sethnickersonmac"
                },
                {
                    "T7": "0"
                }
            ]
        },
这是我的python代码:

response2 = requests.request("POST", url, data=payload2, headers=headers)

j = json.loads(response2.text) 


def find_all(item, level):
    if isinstance(item, dict):
        for k in item:
            (find_all(item[k], level+1))
    else:
        print(item)


def find_only(item, level):
    if isinstance(item, dict):
        for k in item:
            (find_only(item[k], level+1))


for each in j['result']['result']:
    if (find_only(each['Settings'][1], 0)) != json.loads("0"):
        find_all(each['Settings'][0], 0)
相反,我得到了输出中的所有键。我得到以下信息:

shannoncampbell_znyq1
katiekapprelmac
sara-pc_wpv7h
kaelaweeksmac
elenimacbookpro
sethnickersonmac
而不仅仅是Katiekappremac

response2 = requests.request("POST", url, data=payload2, headers=headers)

j = json.loads(response2.text) 


def find_all(item, level):
    if isinstance(item, dict):
        for k in item:
            (find_all(item[k], level+1))
    else:
        print(item)


def find_only(item, level):
    if isinstance(item, dict):
        for k in item:
            (find_only(item[k], level+1))


for each in j['result']['result']:
    if (find_only(each['Settings'][1], 0)) != json.loads("0"):
        find_all(each['Settings'][0], 0)
请帮忙。感谢代码中的

for each in j['result']['result']:
if (find_only(each['Settings'][1], 0)) != json.loads("0"):
    find_all(each['Settings'][0], 0)
我实际上看到,您的条件总是
True
,因为您没有在
find_only()
中返回任何内容

我不知道,你们为什么要用level和这么多递归函数。尽管根据发布的数据很容易提取结果。请查找下面的代码

response2 = requests.request("POST", url, data=payload2, headers=headers)
j = json.loads(response2.text)
for each in j['result']['result']:
if each['Settings'][1]['T7'] not in ["0", 0]:
    print(each['Settings'][0]['AN'])
如果您的回复数据有点复杂,请发布确切的解决方案

如果您有多个密钥名,请查看以下代码:

response2 = requests.request("POST", url, data=payload2, headers=headers)
j = json.loads(response2.text)

def find_all(item):
    if isinstance(item, dict):
        for k in item:
            return item[k]
    # If item is non dict and you want to return this as well on `True`.
    # Uncomment below commented lines.
    # else:
    #     item
def find_only(item):
    if isinstance(item, dict):
        for k in item:
            return item[k]

for each in j['result']['result']:
    if (find_only(each['Settings'][1])) != str(json.loads("0")):
        print(find_all(each['Settings'][0]))

jsonpath ng可以帮助您实现这一点

from jsonpath_ng.ext import parse

found = parse(f"$..Settings").find(data)
if found:
    for i in found:
        if ''.join(i.value[1].values()) != '0':
            print(i.value[0]['AN'])

可能重复感谢您的回复。应该改变什么?谢谢,希望你能得到答案或一些想法。对不起,迟了答复。