将JSON数据传输到Django网站

时间:2011-12-11 09:54:46

标签: python django json

我在带有application/json的java中将HttpPost发送到使用Django的服务器。

我正按照此处所述执行传输:JSON POST request parsing in PHP

如何创建接收此json数据的页面?

2 个答案:

答案 0 :(得分:7)

您可以通过request.raw_post_data

接收json
data=simplejson.loads( request.raw_post_data )

答案 1 :(得分:1)

your views中的request变量包含属性request.POST,其中包含帖子数据。这是(在技术上,就像一个字典)。您还想查看python标准库中的json module

最后我想你会想要像

这样的东西
def my_view(request):
    # error checking omitted here, e.g. what if nothing is posted
    # or the json is invalid etc.
    posted_json = request.POST['my_json_variable']
    my_dict = json.loads(posted_json)

    # do something with the data