Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python解析JSON数组_Python_Arrays_Json - Fatal编程技术网

Python解析JSON数组

Python解析JSON数组,python,arrays,json,Python,Arrays,Json,我正在尝试编写一个小的python脚本,它可以从一个大数据集中解析出数组。我希望从每个对象中提取一些key:value,以便稍后在脚本中回放它们。现在我只想打印结果。我已经成功地使用只包含对象的JSON文件实现了这一点,但它似乎无法用于数组。任何提示都将不胜感激 这是我的密码: # Load up JSON Function import json # Open our JSON file and load it into python input_file = open ('stores-s

我正在尝试编写一个小的python脚本,它可以从一个大数据集中解析出数组。我希望从每个对象中提取一些
key:value
,以便稍后在脚本中回放它们。现在我只想打印结果。我已经成功地使用只包含对象的JSON文件实现了这一点,但它似乎无法用于数组。任何提示都将不胜感激

这是我的密码:

# Load up JSON Function
import json

# Open our JSON file and load it into python
input_file = open ('stores-small.json')
json_array = json.load(input_file)

# Create a variable that will take JSON and put it into a python dictionary
store_details = [
        ["name"],
        ["city"]
    ]

# Learn how to loop better =/
for stores in [item["store_details"] for item in json_array]

# Print my results
print(store_details)
以下是示例JSON数据:

[
  {
    "id": 1000,
    "type": "BigBox",
    "name": "Mall of America",
    "address": "340 W Market",
    "address2": "",
    "city": "Bloomington",
    "state": "MN",
    "zip": "55425",
    "location": {
      "lat": 44.85466,
      "lon": -93.24565
    },
    "hours": "Mon: 10-9:30; Tue: 10-9:30; Wed: 10-9:30; Thurs: 10-9:30; Fri: 10-9:30; Sat: 10-9:30; Sun: 11-7",
    "services": [
      "Geek Squad Services",
      "Best Buy Mobile",
      "Best Buy For Business"
    ]
  },
  {
    "id": 1002,
    "type": "BigBox",
    "name": "Tempe Marketplace",
    "address": "1900 E Rio Salado Pkwy",
    "address2": "",
    "city": "Tempe",
    "state": "AZ",
    "zip": "85281",
    "location": {
      "lat": 33.430729,
      "lon": -111.89966
    },
    "hours": "Mon: 10-9; Tue: 10-9; Wed: 10-9; Thurs: 10-9; Fri: 10-10; Sat: 10-10; Sun: 10-8",
    "services": [
      "Windows Store",
      "Geek Squad Services",
      "Best Buy Mobile",
      "Best Buy For Business"
    ]}
  ]

for
循环语句中,
json\u数组中的每个
item
都是一个字典,字典没有键
store\u details
。所以我稍微修改了一下程序

import json

input_file = open ('stores-small.json')
json_array = json.load(input_file)
store_list = []

for item in json_array:
    store_details = {"name":None, "city":None}
    store_details['name'] = item['name']
    store_details['city'] = item['city']
    store_list.append(store_details)

print(store_list)

谢谢你@yklsga。store_list变量是一个空数组,用于保存正在解析的数据吗?是的
store_list
最初是一个空列表,因为我认为您希望保留
json_数组中的存储顺序。我正在将每个
store\u详细信息
添加到
store\u列表
,以便您以后可以按特定顺序使用它们谢谢您的解释;还在学习,这对我很有帮助!没问题,祝你好运@Romenyr