【问题标题】:Array within json object - How to populate it using pythonjson对象中的数组-如何使用python填充它
【发布时间】:2018-07-07 08:28:47
【问题描述】:

我有以下 json:

post = { "file_name" : file_name, 
  "function" : [{ 
    "func_name" : func_name, 
    "start_line" : start_line, 
    "end_line" : end_line 
  }]
}

我正在尝试使用 python 填充每个文件的函数数组......但我无法......有人可以帮助告诉如何向该数组添加更多函数,假设我们已经有一个带有文件名的条目和该文件中的第一个函数?

【问题讨论】:

  • 为了清楚起见,我正在尝试在浏览文件时填充它并希望向该对象添加函数名称。
  • 查看有关如何使用和附加到列表的文档...这个问题显示没有研究工作。

标签: python json multidimensional-array dynamic


【解决方案1】:

这很简单,因为功能键可以有字典列表,所以请按照以下步骤操作:

post = { "file_name" : file_name, "function" : [{ "func_name" : func_name, "start_line" : start_line, "end_line" : end_line }] }

#Define new function
new_function = { "func_name" : new_func_name, "start_line" : new_start_line, "end_line" : new_end_line }

#Append the new_function into post

post["function"].append(new_function)

print post

希望这会有所帮助。

【讨论】:

  • 完美,如此简单:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 2021-01-13
  • 1970-01-01
  • 2014-09-22
  • 2013-10-31
  • 1970-01-01
  • 2020-10-25
相关资源
最近更新 更多