Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of json coding in python3

Shulou Source: shulou.com Published: 2022-05-31 10:35:28 09月23日 Update

Most people do not understand the knowledge points of this article "what is the role of json coding in python3", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the role of json coding in python3" article.

Here is an example:

In: json.dumps ('Chinese', ensure_ascii=True) Out [121]:'\\ u4e2d\\ u6587''In [122]: json.dumps ('Chinese', ensure_ascii=False) Out [122]: 'Chinese' copy code

You can see that when ensure_ascii is True, Chinese is encoded into Unicode code, which can only be displayed normally for False, but what does this have to do with ASCII? Let's take a look at the official documentation's explanation of this parameter:

If ensure_ascii is true (that is, the default), the output guarantees that all input non-ASCII characters will be escaped. If ensure_ascii is false, these characters are output as is.

Copy the code

Now it is slightly understood that when ensure_ascii is True, if there is a non-ASCII character in the string, it will be escaped. According to the result, we can know that this character has been escaped into Unicode code and formatted into a string. Note that "\\ u4e2d\\ u6587" is different from "\ u4e2d\\ u6587". The former is a string of length 12, and the latter will be directly parsed into Chinese by Python. The length is 2. This is my initial problem, directly escaping the string over the network may not be recognized. For example, Chinese is escaped to\\ u4e2d\\ U6587, and if the server does not know that it is an escaped string, then it is an ordinary string with a length of 12, which is bound to make an error. Setting ensure_ascii to False will not escape and use the original characters.

The above is about the content of this article on "what is the role of json coding in python3". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Character escape string Chinese content code function length code that is situation article knowledge article output different original ordinary that is value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Huawei Shulou Technology Docker Xiaomi