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

huangapple go评论86阅读模式
英文:

error while parsing JSON file, probally a hidden value on the JSON content

问题

我有一个JSON文件:

https://drive.google.com/file/d/1zh_fJJNWs9GaPnlLZ459twSubsYkzMi5/view?usp=share_link

一开始看起来正常,甚至使用在线的json模式验证器也没有问题。
然而,当我在本地解析它时,出现了错误。
我尝试了使用Python、Node.js和Go语言,但都无法正常工作。
我认为它可能有一些隐藏的值,导致无法解析它。

英文:

I have this JSON file:

https://drive.google.com/file/d/1zh_fJJNWs9GaPnlLZ459twSubsYkzMi5/view?usp=share_link

Looks normal at first, even using online json schema validators
However when parsing it locally, I get error.
I tried it with python, nodejs and golang but It's not working.
I think it probably has some hidden value that make impossible to parse it

答案1

得分: 1

这是完整的解决方案。对代码进行了注释。

# 以字节形式读取文件
import chardet
import json
file_path = r"2022_2973.json"
with open(file_path, "rb") as f:
    data = f.read()  # 以字节形式读取文件
file_encoding = chardet.detect(data)['encoding']  # 检测编码格式
print(f"文件(字节)编码:{file_encoding}")  # 打印编码格式

json_data = json.loads(data.decode(file_encoding))  # 解码字节并加载JSON数据
json_data['snaps'][1]

输出结果:

文件字节编码UTF-16
{'group': 'Slot',
 'group_order': 1,
 'positions': [{'group': 'Slot',
   'position': 'SLWR',
   ...

请注意,这只是代码的翻译部分,不包括回答你的问题。

英文:

here is the complete solution. comments added against the code.

# 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]

output:

file(bytes) encoding:UTF-16
{'group': 'Slot',
 'group_order': 1,
 'positions': [{'group': 'Slot',
   'position': 'SLWR',

huangapple
  • 本文由 发表于 2023年1月27日 08:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75253022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定