我怎么编码这是JSON格式(多个列表)

时间:2017-12-15 21:44:21

标签: python json encoding

我有6个不同的数据列表a [],b [],c [],d [],e [],f []。我将如何将其编码为JSON格式,因此它如下所示:

{
   "date":a[0]
     {
       "value1":b[0]
       "value2":c[0]
       "value3":d[0]
       "value4":e[0]
       "value5":f[0]
     }
   "date":a[1]
     {
       "value1":b[1]
       "value2":c[1]
       "value3":d[1]
       "value4":e[1]
       "value5":f[1]
     }
}

1 个答案:

答案 0 :(得分:1)

您发布的内容并非有效的JSON。我怀疑你真的想要:

{
   a[0]:
     {
       "value1":b[0]
       "value2":c[0]
       "value3":d[0]
       "value4":e[0]
       "value5":f[0]
     },
   a[1]:
     {
       "value1":b[1]
       "value2":c[1]
       "value3":d[1]
       "value4":e[1]
       "value5":f[1]
     }
}

您可以通过循环遍历所有列表来完成此操作。

import json
result = {}
for date, value1, value2, value3, value4, value5 in zip(a, b, c, d, e, f):
    result[date] = { "value1": value1, "value2": value2, "value3": value3, "value4": value4, "value5": value5 }
json_string = json.dumps(result)