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

查看:30
本文介绍了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'])

事实上,进入 process() 的值可能是另一个键值对字典,而不仅仅是一个数字(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 dumps、loads、items、get 方法,但都没有帮助.

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天全站免登陆