SoFunction
Updated on 2024-11-12

django realize HttpResponse return json data for Chinese

Python3 read and write json Chinese garbled problem

Before I used django generally use JsonResponse to return the json data format

But found that the return Chinese will be garbled

from  import JsonResponse
def test(request):
 result = {"result": 0, "msg": "Implementation successful."}
 return return JsonResponse(result)

This way to return simple, but the Chinese will be garbled

Now change to use HttpResponse to return, show Chinese success!

from  import HttpResponse
import json
def test(request):
 result = {"result": 0, "msg": "Implementation successful."}
 #json return to Chinese
 return HttpResponse((result,ensure_ascii=False),content_type="application/json,charset=utf-8")

Additional knowledge:HttpResponse and JsonResponse in Django

We are writing some interface functions, often need to return to the caller json format data, then how to return directly parsable data?

First the first way:

from  import render
from  import HttpResponse,JsonResponse
import json

# Create your views here.

def index(request):
 data={
  'name':'zhangsan',
  'age':18,
 }
 return HttpResponse((data))

Here in the return information of the foreground, the return Content-Type: is text/html, that is, the string type of return, so this return value is not a standard json data, is a string that looks like json data, of course, can be directly converted to json through the tool, but since it is a json interface, then we throw the data is naturally the best json format, that how to throw the standard json format data?

Slightly modify the code, in the HttpResponse to add content_type type json attribute

from  import render
from  import HttpResponse,JsonResponse
import json

# Create your views here.

def index(request):
 data={
  'name':'zhangsan',
  'age':18,
 }
 return HttpResponse((data),content_type="application/json")

Now the return is application/json;

Then Django provides a more convenient method that is JsonResponse, which is built-in to help us encapsulate the operation of this conversion, that is, our interface throws json data, then the HttpResponse to replace the JsonResponse on the OK!

1. First pass dict data:

from  import render
from  import HttpResponse,JsonResponse

# Create your views here.

def index(request):
 data={
  'name':'zhangsan',
  'age':18,
 }
 return JsonResponse(data)

Successfully received json data;

2. Then try list data again:

from  import render
from  import HttpResponse,JsonResponse

# Create your views here.

def index(request):

 listdata=[1,2,3,4,5]
 return JsonResponse(listdata)

Checking the output at this point, it reports an error:

In order to allow non-dict objects to be serialized set the safe parameter to False.

So if we need to make a JsonResponse pass with non-dict data, we need to set the safe parameter to False

from  import render
from  import HttpResponse,JsonResponse

# Create your views here.

def index(request):

 listdata=[1,2,3,4,5]
 return JsonResponse(listdata,safe=False)

Data is successfully received at this point.

3. If we need to use JsonResponse to pass Chinese

def func(request):
 data={'Name':'Shimingkong'}
 return JsonResponse(data,json_dumps_params={'ensure_ascii':False})

At this time you need to add 'json_dumps_params={'ensure_ascii':False}', because the json serialization of Chinese is ascii encoding, so the Chinese passed to the foreground of the Chinese is ascii character code, need to be converted to Chinese in this step.

Above this django realize HttpResponse return json data as Chinese is all I share with you, I hope to give you a reference, and I hope you support me more.