JSON-anytree.exporter 的编码不支持西里尔字母

JSON-Encoding for anytree.exporter does not support cyrillic

我使用 Anytree (https://pypi.org/project/anytree/2.8.0/) 在 Python 中构建了树。 然后我将它导出到 JSON。从文件导入树后,我得到了正常的符号。 问题是 JSON-文件包含 'bad' 符号而不是正常的西里尔文。这是一个代码示例:

  1. 构建树
  2. 将树导出到 json + 渲染
  3. 从 json + 渲染导入树
  4. 打印json内容

代码:

import smart_open
import json, pprint
from anytree import AnyNode, RenderTree
from anytree.exporter import JsonExporter
from anytree.importer import JsonImporter
json_file = r'd:\temp\test.json'
#building tree
root = AnyNode(uk = 'узел-0', parent=None)
b = AnyNode(uk = 'узел-01', parent=root)
a = AnyNode(uk = 'узел-011',parent=b)
g = AnyNode(uk = 'узел-021', parent = root)
print()
print('Tree for export:')
print(RenderTree(root).by_attr('uk'))
#export
exporter = JsonExporter(indent=2, sort_keys=True)
data = exporter.export(root)
with open(json_file, 'w', encoding='utf-8') as outfile:
    json.dump(data, outfile, ensure_ascii=False)
#import
importer = JsonImporter()
with open(json_file, encoding='utf-8') as json_file:
    data = json.load(json_file)
root = importer.import_(data)
print()
print('Imported tree:')
print(RenderTree(root).by_attr('uk'))
#print bad json
print()
print('data in JSON-file:')
pprint.pprint(data)

结果是:

> Tree for export: 
> узел-0 
> ├── узел-01 
> │   └── узел-011 
> └── узел-021
> 
> Imported tree:
> узел-0 
> ├── узел-01 
> │   └── узел-011 
> └── узел-021
> 
> data in JSON-file: 
> ('{\n'
>   '  "children": [\n'
>   '    {\n'
>   '      "children": [\n'
>   '        {\n'
>   '          "uk":"\u0443\u0437\u0435\u043b-011"\n'
>   '        }\n'
>   '      ],\n'
>   '  "uk": "\u0443\u0437\u0435\u043b-01"\n'
>   '    },\n'
>   '    {\n'
>   '  "uk": "\u0443\u0437\u0435\u043b-021"\n'
>   '    }\n'
>   '  ],\n'
>   '  "uk": "\u0443\u0437\u0435\u043b-0"\n'
>   '}')

虽然 将西里尔文写入 JSON 是可以的 。示例:

import json, pprint
data = '''
 {
  "uk":"узел-0",
  "children": [
    {
      "uk": "узел-01",
    }
  ]
}'''
json_file = r'd:\temp\test_ru.json'
with open(json_file, 'w', encoding='utf-8') as outfile:
    json.dump(data, outfile, ensure_ascii=False)
with open(json_file, encoding='utf-8') as outfile:
    data = json.load(outfile) 
pprint.pprint(data)

returns:

> ('\n'
>  ' {\n'
>  '  "uk":"узел-0",\n'
>  '  "children": [\n'
>  '    {\n'
>  '      "uk": "узел-01",\n'
>  '    }\n'
>  '  ]\n'
>  '}')

答案是:

JsonExporter(indent=2, sort_keys=True, ensure_ascii=False)