简体   繁体   English

如何在 python 中打印 json 值

[英]How to print json value in python

I have this json value and want print the number for day.我有这个 json 值,想打印一天的数字。 Am able to print the content for the android json value but I want to print only the value for the day which is 70 .我能够打印 android json 值的内容,但我只想打印当天的值70

import json
import requests


json_data = [
    {
        "name": "Android",
        "classification": "public",
        "detail": "Tag1, Tag2, Tag3",
        "days": 70,
        "date_last": "2017-07-21 05:48:07 AM"
    },
    {
        "name": "iPhone",
        "classification": "public",
        "detail": "Tag4, Tag5, Tag6",
        "days": 75,
        "date_last": "2017-07-21 05:48:07 AM"
    },
    {
        "name": "Microsoft",
        "classification": "public",
        "detail": "Tag3, Tag8, Tag9",
        "days": 90,
        "date_last": "2017-07-21 05:48:07 AM"
    }
]


output = [x for x in json_data if x["name"]=="Android"]
print(output)

This is my result with the above code这是我上面代码的结果

[{'name': 'Android', 'classification': 'public', 'detail': 'Tag1, Tag2, Tag3', 'days': 70, 'date_last': '2017-07-21 05:48:07 AM'}]

You need to specify which part of the object you want to add to the output array.您需要指定要将 object 的哪一部分添加到 output 数组中。 In your case this is fairly simple.在您的情况下,这相当简单。 Just add specify the key (in this case days).只需添加指定密钥(在本例中为天)。 It should look like this:它应该如下所示:

output = [x["days"] for x in json_data if x["name"]=="Android"]

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

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