简体   繁体   English

JSON 文件解析错误,同时尝试使用 Python 打印某些值(json.decoder.JSONDecodeError:额外数据:第 2 行第 1 列(字符 20))

[英]JSON files parsing error while trying to print certain values using Python (json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 20))

I am new to Python, and I want to convert a JSON file and print it to the console.我是 Python 的新手,我想转换一个 JSON 文件并将其打印到控制台。 When I try to print the whole JSON it is throwing当我尝试打印整个 JSON 时,它正在抛出

json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 20) json.decoder.JSONDecodeError:额外数据:第 2 行第 1 列(字符 20)

My code:我的代码:

import json

with open('venv/Vt.json', 'r') as json_file:
    parsed_json = json.load(json_file)
for idd in parsed_json:
    print(idd['t_id'])

My JSON file:我的 JSON 文件:

{"index":{"_id":0}}
{"t_id":0,"timestamp":"2016-06-01T09:23:39Z","stat":"571","mod":"M02"}
{"index":{"_id":1}}
{"t_id":0,"timestamp":"2016-06-01T09:23:39Z","stat":"571","mod":"M02"}

While you wait for a better answer, this code (while not great) solves your issue:在您等待更好的答案时,此代码(虽然不是很好)解决了您的问题:

with open('venv/Vt.json', 'r') as json_file:
    try:
        t_ids = [json.loads(line)['t_id'] for line in json_file.readlines()]
        print(t_ids)
    except Exception:
        pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 json.decoder.JSONDecodeError:额外数据:第2行第1列(字符5357) - json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 5357) python json 到 csv,未完全转换, json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 1184) - python json to csv, Not fully converted, json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 1184) json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4) AND raise JSONDecodeError("Extra data", s, end) - json.decoder.JSONDecodeError: Extra data: line 1 column 5 (char 4) AND raise JSONDecodeError("Extra data", s, end) 从列表中循环时出现 python 错误 json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0) - python error while looping from a list json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 有时在 Python 中使用 googletrans 和 Pandas 会发生错误 - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) error occurs sometimes using googletrans and Pandas in Python json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)python - json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) python Python:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - Python: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) json.decoder.JSONDecodeError:期望值:第2行第1列(char 1)错误 - json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1) error 错误:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0) - ERROR: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Python json.decoder.JSONDecodeError:额外数据 - Python json.decoder.JSONDecodeError: Extra data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM