Django:从QueryDict读取JSON对象数组 [英] Django: Reading Array of JSON objects from QueryDict

查看:581
本文介绍了Django:从QueryDict读取JSON对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过JS在服务器端通过AJAX调用传递复合JSON结构,并在python中将其读取为非常相似的数据结构?

How do I pass a composite JSON structure via AJAX call from JS and on the server side, read it as a "very similar" data structure in python?

我知道可以使用json格式(simplejson等),但是我感觉QueryDict本身格式错误或重新格式化了?

I understand that json formatting can be used (simplejson etc), but I somehow feel that the QueryDict itself is malformed or reformatted in my case?

示例:

通过AJAX将JSON对象数组[{ id:1},{ id:2},{ id:3}]传递给Django视图时, QueryDict的格式为:

When passing an array of JSON objects [{"id": 1},{"id": 2},{"id": 3}] via AJAX to Django view, the QueryDict gets formatted as:

POST:<QueryDict: {u'json_data[0][id]': [u'1'], u'type': [u'clone'], 
u'csrfmiddlewaretoken': [u'69bb3c434ced31ab301ede04bf491ec0'], 
u'json_data[1][id]': [u'2'], u'json_data[2][id]': [u'3']}>

我如何遍历json_data?

How do I even iterate through the json_data?

我想改成这样:

POST:<QueryDict: {u'json_data': [{u'id': [u'1']}, {u'id': [u'2']}, {u'id': [u'3']}]},
u'csrfmiddlewaretoken': [u'69bb3c434ced31ab301ede04bf491ec0'], u'type': [u'clone']>

这样我就可以将QueryDict作为字典来访问,并以列表的形式检索json_data并在特定条件下对其进行处理顺序:也许只是按照顺序列表顺序遍历它们。
类似于:

So that I can access QueryDict as a dictionary and retrieve json_data as a list and process it in a certain order: maybe just iterate through them in sequential list order. Something like:

ret = request.POST
for item in ret['json_data']:
    process(item['id'])

事实上,进入流程的价值()可能是另一个键值对字典,而不只是数字(1、2、3等)

In fact the value that goes into process() could be another dictionary of key value pairs instead of just a number (1,2,3 etc)

Javascript:

var test = [{"id": 1},{"id": 2},{"id": 3}];
$.post(
    "/insert_tc",
    {
      json_data: test,
      "type": 'clone',
      "csrfmiddlewaretoken": $csrf_token
    },  
    function(json) {
        //CALLBACK
    },
    "json"
);  

views.py:

def insert_tc(request):
    if request.method == 'POST':       
    ret = request.POST
    type = ret['type']
    list = ret.getlist(ret)

但列表返回空[]

我尝试了simplejson转储,加载,项,获取方法,但是它们都没有帮助。

I tried simplejson dumps, loads, items, get methods but none of them helped.

我什至尝试了jQuery.param(obj,true),但这不是我想要的(尽管有些接近)。

I even tried jQuery.param( obj, true ), but that's not what I want (although somewhat close).

是否存在通过AJAX来回传递Django<-> JS的复合数据结构的更好/更好的方法?

Is there a different/better way to pass composite data structures back and forth Django <-> JS via AJAX?

推荐答案

您应该使用JSON.stringify()对JSON进行字符串化。这会将JSON对象转换为字符串格式,以便可以在另一端正确解析。另一方面,您将需要使用json.loads()来取消字符串化对象。

You should stringify your JSON using JSON.stringify(). This will convert the JSON Object into string format so it can be parsed correctly on the other end. On the other end you will need to use json.loads() to "unstringify" the object.

javascript

var test = [{"id": 1},{"id": 2},{"id": 3}];
$.post(
    "/insert_tc",
    {
      json_data: JSON.stringify(test),
      "type": 'clone',
      "csrfmiddlewaretoken": $csrf_token
    },  
    function(json) {
        //CALLBACK
    },
    "json"
);  

查看

import json
def insert_tc(request):
    if request.method == 'POST':       
        ret = request.POST
        type = ret['type']
        list = json.loads(ret['json_data'])

这篇关于Django:从QueryDict读取JSON对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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