环境: python2.7.5
with open('test.json', 'r') as f:
    data = json.load(f)
此时 data 为
{u'中文的键': u'中文的值'}
里面的键和值都是 unicode 编码的
能不能让 data 为
{'中文的键': '中文的值'}
就是里面的键和值都是 str 类型
|      1darer      2020-06-24 15:27:14 +08:00 用 python2 你的 str 就是 bytes 啊 | 
|      2gzfrankie      2020-06-24 15:31:38 +08:00 像楼上说的 python2 的 string 是 bytes,只能装 ASCII 的字母,装不了中文。 python3 的 string 就是 python2 的 unicode (直接改了名),然后新弄了 bytes 这个类 | 
|  |      3smallpython OP @gzfrankie  u'中文的值'.encode('utf-8') 之后的类型不就是 str 类型吗? 也就是字节类型, 我理解 Python2 中 str 和字节码就是一个东西 而且即便是英文字母 a json.load 之后也是 u'a'而不是'a' | 
|  |      4smallpython OP @gzfrankie 虽然我可以手动把键和值都取出来手动 encode, 但是我希望 json 库自动完成这个过程 | 
|      5gzfrankie      2020-06-24 15:53:57 +08:00  1 stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-from-json 用 json_load 的 object_hook | 
|      6gzfrankie      2020-06-24 15:55:45 +08:00 google 搜索“python 2 json load unicode to string” 出来第一个就是我那个答案 | 
|  |      7smallpython OP @gzfrankie 感谢 |