我有一个json,我希望通过python获取它的元素。 json看起来像这样:
{"$id":"1","Data":
[{"$id":"2","ArticleId":39511,"Title":"a","Order":39,"TotalCount":0},
{"$id":"3","ArticleId":345921,"Title":"a","Order":21,"TotalCount":0},
{"$id":"4","ArticleId":43289,"Title":"a","Order":18,"TotalCount":0},
{"$id":"5","ArticleId":39540,"Title":"a","Order":16,"TotalCount":0},
{"$id":"6","ArticleId":963167,"Title":"a","Order":15,"TotalCount":0},
{"$id":"7","ArticleId":519284,"Title":"a","Order":14,"TotalCount":0},
{"$id":"8","ArticleId":109699,"Title":"a","Order":13,"TotalCount":0},
{"$id":"9","ArticleId":43466,"Title":"a","Order":12,"TotalCount":0},
{"$id":"10","ArticleId":39510,"Title":"a","Order":10,"TotalCount":0},
{"$id":"11","ArticleId":43307,"Title":"a","Order":9,"TotalCount":0},
{"$id":"12","ArticleId":715048,"Title":"a","Order":9,"TotalCount":0},
{"$id":"13","ArticleId":133952,"Title":"a","Order":8,"TotalCount":0},
{"$id":"14","ArticleId":459712,"Title":"a","Order":8,"TotalCount":0},
{"$id":"15","ArticleId":11503,"Title":"a","Order":7,"TotalCount":0}],"TotalCount":
14,"Id":"0","IsSuccess":true,"Message":"OK","StatusCode":200,"ValidationMessages":
null,"Exception":null}
在python中我使用:
mydata = json.loads(response.text)
现在我想知道如何使用mydata来获取字符串。我的意思是“一个”
答案 0 :(得分:0)
你可以用:
mydata = json.loads(response_text)
titles = [dct['Title'] for dct in mydata['Data']]
帮助计算这样的事情的好方法是先做这样的事情:
print(json.dumps(mydata, indent=4))
在这种情况下,会导致显示如下内容:
{
"Exception": null,
"$id": "1",
"ValidationMessages": null,
"IsSuccess": true,
"TotalCount": 14,
"Message": "OK",
"Data": [
{
"$id": "2",
"TotalCount": 0,
"ArticleId": 39511,
"Order": 39,
"Title": "a"
},
{
"$id": "3",
"TotalCount": 0,
"ArticleId": 345921,
"Order": 21,
"Title": "a"
},
..., etc,
{
"$id": "15",
"TotalCount": 0,
"ArticleId": 11503,
"Order": 7,
"Title": "a"
}
],
"Id": "0",
"StatusCode": 200
}
由此,通常可以相对容易地确定访问对象的任何部分或部分所需的操作。