【问题标题】:How to loop through each JSON block in an array如何遍历数组中的每个 JSON 块
【发布时间】:2019-07-25 08:59:04
【问题描述】:

如何循环遍历数组中的每个 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"
}
]

【问题讨论】:

  • 你能提供一个你想要得到的例子吗?
  • 您的尝试在哪里? StackOverflow 标题“开发人员学习、分享和建立职业生涯的地方”。
  • 您拥有的是 dictionarieslist,而不是 JSON 块array >.

标签: python arrays json python-3.x


【解决方案1】:

试试下面的代码:

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)

希望这能回答你的问题!!!

【讨论】:

  • 感谢您的回复。我有它作为 JSON 数组。所以,它打印如下......每行一个字符...... [ {“名称”:............ ..
  • json 数组是什么意思。你能展示你的意见吗?
  • 我正在解析一个 CSV 文件.. data = [] fieldnames = ("occurrences", "post_title", "ID") reader = csv.DictReader(csvfile, fieldnames)
  • 你能不能也显示你的csv,这样我就可以看到里面有什么样的数据了..
  • 出现次数、post_title、ID 1、测试 9、17 2、测试 8、19
猜你喜欢
  • 2021-03-19
  • 2018-10-05
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 2014-05-08
相关资源
最近更新 更多