Python json loads encounters a Chinese situation analysis. __js

Source: Internet
Author: User
Tags true true python parse json
Python has its own module for handling python, which can be directly import JSON when used.
Using the loads method, you can convert a JSON string into a Python object, as follows:
JSON Python Object dict Array list string Unicode number (int) int, long (real) float true true false null No Ne

One. However, when using the JSON module, it is necessary to pay attention to the processing of Chinese, loads method if the encoding of the incoming string is not UTF-8, you need to use encoding to specify the character encoding

#!/usr/bin/env python
#-*-coding:utf-8-*-

import json
js = json.loads (' {"Insun": "Terry/Man in the awkward way 2/lost in Thai Land "}")
print json.dumps (JS)
print json.dumps (js,ensure_ascii=false)


{"Insun": "\u6cf0\u56e7/\u4eba\u5728\u56e7\u90142/lost in Thailand"}
{"Insun": "Terry/Man in the 2/lost in Thailand"}

Two. If the encoding of the passed-in string is not UTF-8, you need to specify the character encoding with encoding

For:

Datadict = Json.loads (DATAJSONSTR);

Where Datajsonstr is a JSON string, if the encoding itself is UTF-8, such as GB2312, the code above will cause an error. Change to correspond to:

Datadict = Json.loads (datajsonstr, encoding= "GB2312");

It's OK.

Here, that corresponds to the function explained above:

If S is a str instance and are encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then a appropriate en Coding name must be specified

Three. If the string to parse, the encoding type of itself, is not based on ASCII, then, before calling Json.loads, you need to convert the corresponding string to a Unicode type

Or in the above:

Datadict = Json.loads (Datajsonstr, encoding= "GB2312");

For example, even if you datajsonstr the string here, the appropriate encoding has been specified by encoding, but since it contains other encoded characters, such as my own datajsonstr is GB2312 character, but contains some Japanese characters, at this point, Json.loads still goes wrong because DATAJSONSTR is not a character encoding based on ASCII, so you need to convert DATAJSONSTR to Unicode before calling Json.loads.

The code is as follows:

Datajsonstruni = Datajsonstr. Decode ("GB2312");
Datadict = json.loads (datajsonstruni, encoding= "GB2312");



Website:
18.2. Json-json Encoder and Decoder

http://docs.python.org/2/library/json.html json.dumps (obj, Skipkeys=false, Ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=none, Separators=none, encoding= "Utf-8", Default=none, Sort_keys=false, **kw) Json.loads (s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]] ]]])



Reference:

Python json Chinese
Python JSON module decoding bugs in Chinese
http://www.douban.com/note/214822809/

Using python to process JSON
HTTP ://www.giser.net/?p=990

Python json.dumps Chinese display problem resolution
http://blog.csdn.net/sagittar/article/details/ 6169605

Python parse JSON data structure example
http://hi.baidu.com/leejun_2005/item/fc688affc196f8723c198b7c Json.loads parsing a string containing \ n in Python will error
Http://www.crifan.com/use_python_json_loads_parse_string_contain_newline_ will_fail_error/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.