这是格式正确的嵌套字典,还是其他?

时间:2019-06-21 04:37:01

标签: python json python-3.x dictionary

我正在学习Python,并且似乎陷入了涉及嵌套字典基础知识的问题。

我的代码从API中提取数据,但我通过以下结构获得了数据,但是我不确定这是格式正确的嵌套字典吗?

我一直试图理解嵌套字典,我认为嵌套字典必须遵循以下原则:

people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
          2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}}

在我看来,API返回的数据似乎不使用索引。

"Brands": [
  {
   "brand_id": "brand1",
   "brand_display": "Brand One",
   "brand_is_common": "No",
   "brand_country": "UK"
  },
  {
   "brand_id": "brand2",
   "brand_display": "BRand Two",
   "brand_is_common": "Yes",
   "brand_country": "USA"
  },
  {
   "brand_id": "brand3",
   "brand_display": "Brand Three",
   "brand_is_common": "No",
   "brand_country": "UK"
  }, 

等等等

如果我想遍历从API获得的数据,该如何在没有索引的情况下做到这一点?

如何查找没有索引的数据?

2 个答案:

答案 0 :(得分:0)

您可以非常轻松地遍历结果,只需检查以下示例:

api_response = {
  "Brands": [
    {
    "brand_id": "brand1",
    "brand_display": "Brand One",
    "brand_is_common": "No",
    "brand_country": "UK"
    },
    {
    "brand_id": "brand2",
    "brand_display": "BRand Two",
    "brand_is_common": "Yes",
    "brand_country": "USA"
    },
    {
    "brand_id": "brand3",
    "brand_display": "Brand Three",
    "brand_is_common": "No",
    "brand_country": "UK"
    }
  ]
}

for brand in api_response["Brands"]:
  print(brand["brand_id"] + " " + brand["brand_country"])

输出:

brand1 UK
brand2 USA
brand3 UK

答案 1 :(得分:0)

首先,您对嵌套词典的描述和示例是正确的。但是,我不清楚您所显示的数据是什么。在api的示例输出中,代码段以逗号结尾,您应该对其进行编辑,以便人们可以以正确的格式查看整个输出。如当前示例所示,它在python中没有有效的语法。如果实际的api输出与您所显示的完全一样,那么看起来数据是用json格式而不是python格式的。如果要查看如何在python中解析json数据,请检查此答案Deserialize a json string to an object in python

我将假设您从API获得的数据看起来像这样(我删除了逗号并添加了一个大括号,并用花括号将其括起来以使其成为有效的python,我也将其分配给了变量,以便我们可以通过键访问其值):

api_output = { 
"Brands": {[
  {
   "brand_id": "brand1",
   "brand_display": "Brand One",
   "brand_is_common": "No",
   "brand_country": "UK"
  },
  {
   "brand_id": "brand2",
   "brand_display": "BRand Two",
   "brand_is_common": "Yes",
   "brand_country": "USA"
  },
  {
   "brand_id": "brand3",
   "brand_display": "Brand Three",
   "brand_is_common": "No",
   "brand_country": "UK"
  }]} 
}

我们正在查看具有以下结构的字典:


example_dict = {
                 "example_key":{ 
                                 [ #the square brackets denote a list
                                  {"sub_key":sub_value,"sub_key0":sub_value0},  
                                  {"sub_key":sub_value} #these dicts are stored in the list
                                 ]
                               }    
                 }

您的直觉是正确的,api没有返回嵌套的字典。数据格式似乎是带有字符串键的字典,其值是一个列表,其中包含一组字典。

因此,回到您的示例,我们需要浏览列表和字典。要访问列表中的值,请按索引,而要访问字典中的值,请按键。由于词典列表是作为另一本词典的值存储的,因此我们使用它的相应键来访问该列表。

list_of_brands = api_output["Brands"] #assign the value of "Brands" to a variable

#we can check the value type (not needed but useful for understanding whats going on)

type(list_of_brands)

#out: list

现在,我们需要按索引遍历列表以找到其中包含的字典,然后假设要检查每个字典中的每个键,我们将遍历字典中的键并使用值进行操作:< / p>

for brand in list_of_brands: # for each dictionary in the list  
    for key in brand: # loop through the keys of the current dict,
        print(" key: " + key + "\n value: " + brand[key]) #prints the key and its value

将它们放在一起,代码看起来像这样:

api_output = { 
"Brands": {[
  {
   "brand_id": "brand1",
   "brand_display": "Brand One",
   "brand_is_common": "No",
   "brand_country": "UK"
  },
  {
   "brand_id": "brand2",
   "brand_display": "BRand Two",
   "brand_is_common": "Yes",
   "brand_country": "USA"
  },
  {
   "brand_id": "brand3",
   "brand_display": "Brand Three",
   "brand_is_common": "No",
   "brand_country": "UK"
  }]} 
}

for brand in list_of_brands: # for each dictionary in the list  
    for key in brand: # loop through the keys of the current dict,
        print(" key: " + key + "\n value: " + brand[key]) #prints the key and its value

希望这可以帮助您更好地了解发生了什么!