简体   繁体   English

如何将请求数据转换为json或dict格式

[英]How can I convert request data to json or dict format

From the client side, I receive some data by ajax post. 从客户端,我通过ajax发布接收一些数据。 The data type is json format. 数据类型为json格式。

function sendProjectBaseInfo() {

    prj = {
        id : $('#id').val(),
        email : $('#email').val(),
        title : $('#title').val(),
    }

    $.ajax({
        url: '/prj/',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: prj,
        success: function(result) {
            alert(result.Result)
        }
    });
}

After got the json data, I try to convert to json, or dict format. 得到json数据后,我尝试转换为json或dict格式。 To convert to json. 转换为json。 I wrote like this: 我这样写:

import json

def post(self, request):
    if request.is_ajax():
        if request.method == 'POST':
            json_data = json.loads(request.data)
            print('Raw Data : %s'%request.body)
    return HttpResponse('OK!')

in case of above, I got 500 Internal server Error. 在上述情况下,我收到500 Internal server Error。

So I wrote like below to work around this error. 所以我像下面这样写来解决这个错误。

import json

def post(self, request):
    if request.is_ajax():
        if request.method == 'POST':
            data = json.dumps(request.data)
            print('Raw Data : %s'%request.body)
    return HttpResponse('OK!')

After all I got the same error. 毕竟我遇到了同样的错误。 So I was look into the requested data. 因此,我正在调查所需的数据。

import json

def post(self, request):
    if request.is_ajax():
        if request.method == 'POST':
            print('Raw Data : %s'%request.body)
    return HttpResponse('OK!')

Print out is : 打印输出为:

Raw Data : b'{"id":"1","email":"jason@test.co","title":"TEST"}'

How can I overcome this situation? 我该如何克服这种情况?

request handles the data in the form of bytes(data type) so first we need to convert it into string format , after you can convert it into json format . request字节(数据类型)的形式处理数据 ,因此首先需要将其转换为字符串格式 ,然后再将其转换为json格式

import json

def post(self,request):
   if request.is_ajax():
     if request.method == 'POST':
        json_data = json.loads(str(request.body, encoding='utf-8'))
        print(json_data)
   return HttpResponse('OK!')

you must be getting TypeError: the JSON object must be str, not 'bytes' exception. 您必须获取TypeError: the JSON object must be str, not 'bytes'例外。 (is it Python3?) (是Python3吗?)

if yes, then try this before json.loads : .decode(encoding='UTF-8') this is because, the response body is of byte type, if notice small b in the beginning of output string 如果是,则在json.loads.decode(encoding='UTF-8')之前尝试此操作,这是因为,响应主体为byte类型,如果在输出字符串的开头注意小b

if request.method == 'POST':
       json_data = json.loads(request.body.decode(encoding='UTF-8'))
       print('Raw Data : %s' % json_data)
       return HttpResponse('OK!')
$.ajax({
    url: '/prj/',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: prj, #### YOUR ERROR IS HERE
    success: function(result) {
        alert(result.Result)
    }
});

You will need to convert your data into string in your js. 您将需要在js中将数据转换为字符串。

Do the following in your js code 在您的js代码中执行以下操作

data: JSON.stringify(prj)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM