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

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

问题描述

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

  print request.GET 

以下行的输出格式为

 < QueryDict:{你的[我的json数组]':[u''}}> 

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



我的输出应该看起来像这样



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

有关我在这里做错什么的任何提示?

解决方案

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



如果要将其转储为字符串,只需使用 json.dumps()

  json.dumps(my_query_dict)
/ pre>

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


QueryDict.dict()



返回QueryDict的dict表示形式。



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''}}>

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.

My output should look like this

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

Any tips as to what I am doing wrong here?

解决方案

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

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

json.dumps(my_query_dict) 

There is also a relevant dict() method:

QueryDict.dict()

Returns dict representation of QueryDict.

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

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