如何遍历数组中的每个JSON块

时间:2019-03-04 04:24:55

标签: python arrays json python-3.x

如何遍历数组中的每个JSON块?例如,我要遍历每个JSON块(而不是每个元素),并将其作为主体发送到API调用中。

print(json_data)

输出如下。

[
{
   "occurrences": "1",
   "post_title": "Test 9",
   "ID": "17"
},
{
   "occurrences": "2",
   "post_title": "Test 8",
   "ID": "19"
},
{
   "occurrences": "5",
   "post_title": "abc",
   "ID": "11"
}
]

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

import csv

fieldnames = ("occurrences", "post_title", "ID")
api_list = list()

with open("csv_file.csv", 'r') as data_file: # Pass Csv file in here
     reader = csv.DictReader(data_file, fieldnames)
     for i in reader:
         api_list.append(i)


for i in api_list:
    print i  # Will print each JSON Block ( Send it to API from here)

希望这能回答您的问题!