我有这个 JSON 文件:
https://drive.google.com/file/d/1zh_fJJNWs9GaPnlLZ459twSubsYkzMi5/view?usp=share_link
一开始看起来很正常,即使使用在线 json 模式验证器 但是,当在本地解析它时,我收到错误。 我用 python、nodejs 和 golang 尝试过,但它不起作用。 我认为它可能有一些隐藏的价值,使得无法解析它
正确答案
这是完整的解决方案。针对代码添加的注释。
# read the file as bytes
import chardet
import json
file_path=r"2022_2973.json"
with open (file_path , "rb") as f:
data= f.read() # read file as bytes
file_encoding=chardet.detect(data)['encoding'] # detect the encoding
print(f"file(bytes) encoding:{file_encoding}") # print encoding
json_data = json.loads(data.decode(file_encoding)) # decode the bytes and load the json data
json_data['snaps'][1]
输出:
file(bytes) encoding:UTF-16
{'group': 'Slot',
'group_order': 1,
'positions': [{'group': 'Slot',
'position': 'SLWR',