在python的嵌套json字典中查找值 [英] Find a value within nested json dictionary in python

查看:53
本文介绍了在python的嵌套json字典中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

一行json

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

您将如何获得文本"的值?(我知道如何使用 json.loads 加载 json).但我不确定如何获取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.

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

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

推荐答案

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

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'

您基本上将其视为字典,传递键以获取每个嵌套字典的值.唯一不同的部分是当您点击 maindata 时,结果值是一个列表.为了处理这个问题,我们拉取第一个元素 [0] 然后访问 Info 键以获取值 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 Info key to get the value TEXT.

如果 unknown 发生变化,您可以将其替换为一个变量,该变量代表在您的代码中将采用的已知"名称:

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

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

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

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

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

其中 values() 是一个包含以下内容的变量:

Where values() is a variable containing:

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

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

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.

这篇关于在python的嵌套json字典中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆