我必须从数组中构建一个json,然后通过套接字将其发送到Java应用程序,然后在其中打开它。我有点像
array = ["a","b","c"]
{
"events":[
{"id":array[0], "name":"bla1"},
{"id":array[1], "name":"bla2"}
],
"name": "bla"
}
我尝试使用连接失败。我该怎么办?
答案 0 :(得分:1)
尝试:
o ={
"events":[{"id": item, "name": "blah%s" %(index + 1)}
for index, item in enumerate(array)],
"name": "bla"
}
print(o)
# {'events': [{'id': 'a', 'name': 'blah1'}, {'id': 'b', 'name': 'blah2'}, {'id': 'c', 'name': 'blah3'}], 'name': 'bla'}