在python中的嵌套json字典中查找值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14227561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:49:11  来源:igfitidea点击:

Find a value within nested json dictionary in python

pythondictionarypython-2.7nested

提问by user1959942

From the following json, in python, I'd like to extract the value "TEXT". All the keys are constant except for unknown. Unknown could be any string like "a6784t66" or "hobvp*nfe". The value of unknown is not known, only that it will be in that position in each json response.

从以下 json 中,在 python 中,我想提取值“TEXT”。除了未知之外,所有的键都是不变的。未知可以是任何字符串,如“a6784t66”或“hobvp*nfe”。未知的值是未知的,只是它会在每个 json 响应中的那个位置。

{
  "A": {
    "B": {
      "unknown": {
        "1": "F",
        "maindata": [
          {
            "Info": "TEXT"
          }
        ]
      }
    }
  }
}

one line json

一行json

'{"A":{"B":{"unknown":{"1":"F","maindata":[{"Info":"TEXT"}]}}}}'

How would you get the value of "Text"? (I know how to load the json with json.loads)..but I'm not sure how to get the value of "Text". Thanks.

你将如何获得“文本”的价值?(我知道如何使用 json.loads 加载 json)。但我不确定如何获取“Text”的值。谢谢。

(I'm not sure what the best title is.)

(我不确定最好的标题是什么。)

采纳答案by RocketDonkey

It is a bit lenghty, but in that example above:

它有点冗长,但在上面的例子中:

In [1]: import json

In [2]: s = """\
   ...: {
   ...:   "A": {
   ...:     "B": {
   ...:       "unknown": {
   ...:         "1": "F",
   ...:         "maindata": [
   ...:           {
   ...:             "Info": "TEXT"
   ...:           }
   ...:         ]
   ...:       }
   ...:     }
   ...:   }
   ...: }"""

In [3]: data = json.loads(s)

In [4]: data['A']['B']['unknown']['maindata'][0]['Info']
Out[4]: u'TEXT'

You basically treat it as a dictionary, passing the keys to get the values of each nested dictionary. The only different part is when you hit maindata, where the resulting value is a list. In order to handle that, we pull the first element [0]and then access the Infokey to get the value TEXT.

您基本上将其视为字典,传递键以获取每个嵌套字典的值。唯一不同的部分是当您点击 时maindata,结果值是一个列表。为了解决这个问题,我们拉取第一个元素[0],然后访问Info键以获取值TEXT

In the case of unknownchanging, you would replace it with a variable that represents the 'known' name it will take at that point in your code:

unknown更改的情况下,您可以将其替换为一个变量,该变量代表在代码中此时将采用的“已知”名称:

my_variable = 'some_name'
data['A']['B'][my_variable]['maindata'][0]['Info']

And if I would have actually read your question properly the first time, if you don't know what unknownis at any point, you can do something like this:

如果我第一次真正正确地阅读了您的问题,如果您unknown在任何时候都不知道是什么,您可以执行以下操作:

data['A']['B'].values()[0]['maindata'][0]['Info']

Where values()is a variable containing:

values()包含以下内容的变量在哪里:

[{u'1': u'F', u'maindata': [{u'Info': u'TEXT'}]}]

A single-item list that can be accessed with [0]and then you can proceed as above. Note that this is dependent on there only being one item present in that dictionary - you would need to adjust a bit if there were more.

可以访问的单项列表,[0]然后您可以按上述方式进行。请注意,这取决于该字典中只有一个项目 - 如果有更多项目,您需要稍微调整一下。

回答by minocha

As you said that unknown was at a fixed place You can do the following

正如你所说的未知是在一个固定的地方你可以做以下

import json
s=json.loads('{"A":{"B":{"unknown":{"1":"F","maindata":[{"Info":"TEXT"}]}}}}')
i=s["A"]["B"].keys()
x=i[0]   # Will store 'unknown' in x, whatever unknown is
print s['A']['B'][x]['maindata'][0]['Info']    #here x dictionary index is used after B as its value will be the value for unknown

This should do the job, since only the unknown key is really 'unknown'

这应该可以完成工作,因为只有未知的密钥才是真正的“未知”

回答by nagordon

You can use a recursive function to dig through every layer and print its value with an indent

您可以使用递归函数挖掘每一层并用缩进打印其值

def recurse_keys(df, indent = '  '):
    ''' 
    import json, requests, pandas
    r = requests.post(...)  
    rj = r.json() # json decode results query
    j = json.dumps(rj, sort_keys=True,indent=2)            
    df1 = pandas.read_json(j)         
    '''
    for key in df.keys():
        print(indent+str(key))
        if isinstance(df[key], dict):
            recurse_keys(df[key], indent+'   ')
recurse_keys(df1)