繁体   English   中英

如何从 JSON 文件中提取单个 Key 值?

[英]How to extract a single Key value from a JSON file?

这是我的 output.json 文件:

    {
 "ParsedResults": [
  {
   "TextOverlay": {
    "Lines": [],
    "HasOverlay": false,
    "Message": "Text overlay is not provided as it is not requested"
   },
   "TextOrientation": "0",
   "FileParseExitCode": 1,
   "ParsedText": "180 Grade IV\r\n\u0103\u021ar: Class VIII Pass\r\nwww.facebook.com, Since 2012\r\n",
   "ErrorMessage": "",
   "ErrorDetails": ""
  }
 ],
 "OCRExitCode": 1,
 "IsErroredOnProcessing": false,
 "ProcessingTimeInMilliseconds": "343",
 "SearchablePDFURL": "Searchable PDF not generated as it was not requested."
}

我正在尝试从此 JSON 文件中获取ParsedText值。

这是我正在使用的代码:

import json

f = open('output.json',)
data = json.load(f)
print(data['ParsedResults']['TextOverlay']['ParsedText'])
f.close()

面对这个错误:

TypeError: list indices must be integers or slices, not str

如何从ParsedText读取该特定值,请指导。 提前致谢

ParsedResults 不是一个对象,它是一个列表

尝试这个:

import json

f = open('output.json',)
data = json.load(f)
print(data['ParsedResults'][0]['ParsedText'])
f.close()

data['ParsedResults'] 是一个列表,你需要使用索引来解析,所以你得到了 TypeError:列表索引必须是整数或切片,而不是 str

使用数据['ParsedResults'][0]。

使用以下,

import json
f = open('output.json',)
data = json.load(f)
print(data['ParsedResults'][0]['TextOverlay']['ParsedText'])
f.close()

暂无
暂无

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

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