【发布时间】:2020-01-10 02:09:49
【问题描述】:
我想构建类似这样的 json
{
"meta":[
{
"key1": 12345,
"key2": "berlin",
"key3": "best place",
"key4": 58
},
{
"key1": 6789,
"key2": "bangldesh",
"key3": "great place",
"key4": 58
}
]
}
我正在一个名为 mylist 的列表中获取相关数据。 mylist 可能有 n 个大小
<class 'list'>: [('berlin', 12345, 'best place', 58), ('bangldesh', 6789, 'great place', 58)]
我也有键变量作为键列表。 keylist 可以是 n 大小
<class 'list'>: [('key1',), ('key2',), ('key3',), ('key4',)]
我尝试将 json 构建为
count = 0
for i in range(0,maxLen):
key = keylist[count][0]
value = mylist[0][count]
data[key] = value
print(data)
count = count + 1
jsonResult = json.dumps(data)
我收到名称错误
【问题讨论】:
-
这不只是your earlier question 的复制品吗?这些结构只是微不足道不同。
-
@T.J.Crowder:我认为它是一个
fmap变体,它似乎被任意列名提升了。 -
非常感谢 Williem Van Onsem。它完全按预期工作
标签: json django python-3.x django-rest-framework