卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34128本站已运行391

解析 JSON 文件时出错,可能是 JSON 内容的隐藏值

解析 json 文件时出错,可能是 json 内容的隐藏值

问题内容

我有这个 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',
卓越飞翔博客
上一篇: 如何以编程方式为 Prometheus 创建 tsdb wal 或块
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏