繁体   English   中英

如何使用 json.load 读取 json 文件

[英]how to read json file with json.load

我想在这个 json 中选择“ocr_text”

如何使用 json.loads 选择 ocr_text

{'message': 'Success', 'result': [{'message': 'Success', 'input': '1.jpg', 'prediction': [{'id': 'a6447ad9-80f7-4bce-bb5e-588bef3874e6', 'label': 'number_plate', 'xmin': 93, 'ymin': 405, 'xmax': 248, 'ymax': 445, 'score': 0.99992895, **'ocr_text': 'MH 02 CB 4545'**, 'type': 'field', 'status': 'correctly_predicted', 'page_no': 0, 'label_id': '45aaf761-4b60-42e9-b9a7-21d7ea8b927a'}], auto=compress&expires=1670532718&or=90&s=373803a82f093ab6b3b68d530f85f294', 'original_with_long_expiry': 'https://nnts.imgix.net/uploadedfiles/59aedc47-df0d-4e93-a52d-dd7076da1287/PredictionImages/658c79d6-c4c7-4ce3-8dfc-41d8884d5719.jpeg?expires=1686070318&or=0&s=849652a08454ccca0ac5cfb779c0cba3'}, 'uploadedfiles/59aedc47-df0d-4e93-a52d-dd7076da1287/RawPredictions/1-2022-12-08T16-51-56.347.jpg': {'original': 'https://nanonets.s3.us-west-2.amazonaws.com/uploadedfiles/59aedc47-df0d-4e93-a52d-dd7076da1287/RawPredictions/1-2022-12-08T16-51-56.347.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA5F4WPNNTLX3QHN4W%2F20221208%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20221208T165158Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&response-cache-control=no-cache&X-Amz-Signature=6ccfc59eb43ffe89dda229ca2a91f09f883596014c7ab0bba6028432f506438d', 'original_compressed': '', 'thumbnail': '', 'acw_rotate_90': '', 'acw_rotate_180': '', 'acw_rotate_270': '', 'original_with_long_expiry': ''}}}

你提供的JSON有错误,不过我会尽力解答。

如果 json 数据在代码中的字符串内,您可以像这样使用loads

import json

json_string = ...  # this is the string with json data
json_dict = json.loads(json_string)

print(json_dict["ocr_text"])

首先,我使用json.loads将 json 数据从json_string到字典 ( json_dict )
然后,我将其视为带有方括号的常规字典。

如果您愿意,python json 模块文档有更多信息。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM