简体   繁体   English

如何使用python获取json子类

[英]How to get a json subclass with python

i have the following json file, the "ticket" python variable contains the "data" part: 我有以下json文件,“票证” python变量包含“数据”部分:

{
"date": "139096543" 
"data": [
    {
    "author": {
                "title": "User",
                "champion": false,
                "canonical_name": "tahlia_moffitt",
                "id": 6944932
    },
    "is_spam": false,
    "me_too_count": 2,
    "status": "complete"
    }
]
}

I used the following python script to get the data for example from "status": 我使用以下python脚本从“状态”获取数据,例如:

'status': ticket.get('status') or 'NoStatus'

But i need the data from the author subclass. 但是我需要来自author子类的数据。 I tried as: 我尝试为:

'title': ticket.get('author.title') or 'NoTitle'

and

'title': ticket.get('author[title]') or 'NoTitle'

too, but they didnt work for me. 也,但他们没有为我工作。 Can anybody propose a solution for me? 有人可以为我提出解决方案吗?

Try this construction: 试试这个构造:

'title': ticket.get('author',{}).get(title', 'NoTitle')

Construction 施工

get(title', 'NoTitle')

Will return value if present, if not will return second value. 如果存在,将返回值,如果不存在,将返回第二个值。

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

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