【发布时间】:2015-09-28 00:57:09
【问题描述】:
我有一个如下所示的 JSON 对象:
[
{
"modulename": "module1",
"functions": [
{
"functionname": "get",
"function": "function1"
},
{
"functionname": "delete",
"function": "function2"
}
]
}
]
我的代码如下所示:
modules = []
for functionname in function_dictionary:
modulename = function_dictionary[function][1]
func = function_dictionary[function][0]
module_func = {"modulename" : modulename, "functions" : [{"functionname" : functionname, "function" : func}]}
found = False
for module in modules:
if module["modulename"] == modulename:
module["modulename"]["functions"].append({"functionname" : functionname, "function" : func}) #error here
found = True
break
if not found:
modules.append(module_func)
但是我不断收到string indices must be integers, not str 错误。
我不知道为什么会这样。
我将其解读为“将对象 {"functionname" : function, "function" : func} 附加到位于 module["modulename"]["functions"] 的列表中
欢迎任何建议!
【问题讨论】:
-
替换
module["modulename"]["functions"].append为module["functions"].append;module["modulename"]确实是一个字符串
标签: python json list dictionary multidimensional-array