Javascript 在Django中将JSONified对象传递到后端?

Javascript 在Django中将JSONified对象传递到后端?,javascript,jquery,json,ajax,django,Javascript,Jquery,Json,Ajax,Django,我试图将js中创建的对象转换为JSON,然后用Django传递到我的后端 首先,我列出一个列表: function get_account_info(){ var account_list = []; $('.card').each(function(e){ var account = []; account.push($(this).find('.name').val()); account.push($(this).find('

我试图将js中创建的对象转换为JSON,然后用Django传递到我的后端

首先,我列出一个列表:

function get_account_info(){
    var account_list = [];
    $('.card').each(function(e){
        var account = [];
        account.push($(this).find('.name').val());
        account.push($(this).find('.username').val());
        account.push($(this).find('.password').val());

        if(account[0] != null){
            account_list.push(account);
        }
    })
    return account_list;
}
然后我试着把它贴出来:

var account_info_json = JSON.parse(get_account_info());
        $.ajax({
            type:'POST',
            url:'/create_new_group/create_group/',
            data:{
                csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
                account_info: account_info_json,
            } ,
            success:function(data){
                    if(data.status == 1){
                            //success!
                            console.log('Success!')
                    }
                    else if(data.status == 2){
                            //failed
                            console.log('Failed!')
                    }
            }
这就是我在views.py中打印(json.dumps(request.POST))时得到的结果

{"csrfmiddlewaretoken": "token_data_is_here", "account_info": "[[\"1111111\",\"1111111\",\"1111111\"],[\"222222\",\"222222\",\"222222\"]]"}

我只能像stirng一样访问这些数据,而不是JSON。如何使其像JSON一样访问?

您必须首先反序列化JSON字符串:

import json
account_info = json.loads(request.POST['account_info'])

您必须首先反序列化json字符串:

import json
account_info = json.loads(request.POST['account_info'])

在发送之前,所有数据都将转换为字符串,因此当涉及到django时,您需要首先将字符串转换为json

要将字符串转换为json,请执行以下操作:

import json
str = json.loads(json_data)
要从字符串获取json,请执行以下操作:

import json
json_data = json.loads(str)
在本例中,您需要先将字符串转换为json,然后才能将其作为python中的字典进行访问


希望能有帮助

在发送之前,所有数据都将转换为字符串,因此,对于django,您需要首先将字符串转换为json

要将字符串转换为json,请执行以下操作:

import json
str = json.loads(json_data)
要从字符串获取json,请执行以下操作:

import json
json_data = json.loads(str)
在本例中,您需要先将字符串转换为json,然后才能将其作为python中的字典进行访问

希望能有帮助