如何用json.loads读取JSON文件

3pvhb19x  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(157)

我想在这个json中得到“ocr_text”
我怎样才能得到ocr_text例如:json.loads(response.text)[“名称”]

{
    "name": "jane doe",
    "salary": 9000,
    "skills": [{
        "Raspberry pi":" MHSO",
        "Machine Learning": "MHSO",
        "Web Development": "uaskdj",
        "ocr_text": "MH 02 CB 4545"
    }],
    "email": "JaneDoe@pynative.com",
    "projects": [
        "Python Data Mining",
        "Python Data Science"
    ]
}
yhuiod9q

yhuiod9q1#

import json

jsonstr = '{ "name": "jane doe", "salary": 9000, "skills": [{ "Raspberry pi":" MHSO", "Machine Learning": "MHSO", "Web Development": "uaskdj", "ocr_text": "MH 02 CB 4545" }], "email": "JaneDoe@pynative.com", "projects": [ "Python Data Mining", "Python Data Science" ] }'
j = json.loads(jsonstr)

ocr = j["skills"][0]["ocr_text"]
o75abkj4

o75abkj42#

将其粘贴到python shell中并进行实验

>>> data = {
...     "name": "jane doe",
...     "salary": 9000,
...     "skills": [{
...         "Raspberry pi":" MHSO",
...         "Machine Learning": "MHSO",
...         "Web Development": "uaskdj",
...         "ocr_text": "MH 02 CB 4545"
...     }],
...     "email": "JaneDoe@pynative.com",
...     "projects": [
...         "Python Data Mining",
...         "Python Data Science"
...     ]
... }
>>> 
>>> data["skills"]
[{'Raspberry pi': ' MHSO', 'Machine Learning': 'MHSO', 'Web Development': 'uaskdj', 'ocr_text': 'MH 02 CB 4545'}]
>>> data["skills"][0]
{'Raspberry pi': ' MHSO', 'Machine Learning': 'MHSO', 'Web Development': 'uaskdj', 'ocr_text': 'MH 02 CB 4545'}
>>> data["skills"][0]["ocr_text"]
'MH 02 CB 4545'

字符串
这就对了,它将得到列表中的第一个ocr_text,如果列表中没有项,它将出错,并且它不处理列表中有多个数据项的可能性。

相关问题