Home  >  Q&A  >  body text

python - JSON containing Chinese failed to dump as expected, (\\xxx\\xxx\\xxx)?

Using Python's requests library to imitate a custom web client, I encountered an exception problem after submitting content containing Chinese characters.

After checking the raw of the request with Fiddler, we can see that the submission content of the official client is:

...
{"jsonstr":"{\"pageindex\":1,\"keyword\":\"张三\"}"}

The self-defined client submission content is:

...
{"jsonstr": "{'pageindex': 1,'keyword': '\xe5\xbc\xa0\xe4\xb8\x89'"}

The script for customizing the client is probably:

# -*- coding: utf-8 -*-

keyword ='张三'
jsonstr ={ "pageindex":1,"keyword":keyword}
data = {"jsonstr":str(jsonstr)}

r = requests.post(url, data =json.dumps(data))

Try data =json.dumps(data, ensure_ascii=False) but the situation remains the same.
If the keyword is a number, everything is normal.
The platform used is win7.

漂亮男人漂亮男人2711 days ago686

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-18 11:00:57

    # coding: utf-8
    
    import json
    
    keyword = '张三'
    jsonstr ={ "pageindex":1,"keyword":keyword}
    data = {"jsonstr": json.dumps(jsonstr, ensure_ascii=False)}
    
    r = requests.post(url, json=data)

    reply
    0
  • Cancelreply