本文介绍了如何将JSON框架列表转换为JSON框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要转换JSON对象列表或单个JSON框架

I want to convert List of JSON object ot Single JSON frame

这是我的代码

for i in user1:
    name=i.name
    password=i.password
    id1=i.id
    user = { "name" : name,
              "password" : password,
              "id":id1}
    user = json.dumps(user)
    userid.append(user)

我得到的输出是

我想要的是

推荐答案

json.dumps()返回一个字符串,因此您将字符串追加到userid -因此最终显示了字符串列表.

json.dumps() returns a string, so you're append strings to userid—and thus end up with the list of strings shown.

如果您想要使用id作为主键的字典词典,则只需:

If you want a dictionary of dictionaries using the id as the primary key, all you need is:

users = {i.id: {"name": i.name, "password": i.password} for i in user1}

这篇关于如何将JSON框架列表转换为JSON框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:26
查看更多