Python:查询字典到 JSON [英] Python: Query Dict to JSON

查看:28
本文介绍了Python:查询字典到 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 encodeURIComponent() 在查询字符串中将一些 JSON 发送到我的 django 应用程序,我的服务器端点接收数据很好,因为我可以将其打印到 python 控制台.

I am trying to send some JSON to my django app in a query string by using encodeURIComponent() my server enpoint receives the data just fine as I can print it to the python console.

print request.GET

下面一行的输出就是这种格式

The output of the following line is in this format

<QueryDict: {u'[my json array]': [u''}}>

我想将其转换为 JSON 以便我可以用来获取一些信息,但我已经尝试使用 json.loads 和其他操作数据的方法,但没有运气.

I want to convert this to JSON so I can use to get some information but I've tried using json.loads and other means of manipulating the data with no luck.

我的输出应该是这样的

[{u'something': something}, {u'something1': something2}, {u'something3': something3}]

关于我在这里做错的任何提示?

Any tips as to what I am doing wrong here?

推荐答案

QueryDict 类是子类常规 Python 字典,除了它处理同一个键的多个值(参见 code>MultiValueDict 实现).

QueryDict class is a subclass of regular Python dictionary, except that it handles multiple values for a same key (see MultiValueDict implementation).

如果你想把它转储到一个字符串,只需使用json.dumps():

If you want to dump it to a string, just use json.dumps():

json.dumps(my_query_dict) 

还有一个相关的dict() 方法:

There is also a relevant dict() method:

QueryDict.dict()

QueryDict.dict()

返回 QueryDict 的字典表示.

Returns dict representation of QueryDict.

这篇关于Python:查询字典到 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆