简体   繁体   English

如何从 JSON 响应中提取单个值(使用 Python)?

[英]How to extract a single value from JSON response (with Python)?

Hi I'm working on a project with a colleague of mine but we stopped.嗨,我正在和我的一位同事一起做一个项目,但我们停止了。
We can't extract a single element of a JSON response.我们无法提取 JSON 响应的单个元素。
We have to extrapolate the "mood" attribute from the code that I am attaching, thanks to all in advance:我们必须从我附加的代码中推断出"mood"属性,提前感谢大家:

{
    u'operation_id': u'5526bd03b2e448ea8d2f3e96ae5ba88c', 
    u'photos': [
    {
        u'height': 280,
        u'pid': u'F@01aa37a755638d436b6d527a18da96bc_ffc4e38fc72b2',
        u'tags': [
        {
            u'attributes':
            {
                u'age_est':
                {
                    u'confidence': 50,
                    u'value': u'27'
                },
                u'anger':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'beard':
                {
                    u'confidence': 91,
                    u'value': u'true'
                },
                u'dark_glasses':
                {
                    u'confidence': 48,
                    u'value': u'false'
                },
                u'disgust':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'ethnicity':
                {
                    u'asian':
                    {
                        u'confidence': 7,
                        u'value': u'false'
                    },
                    u'black':
                    {
                        u'confidence': 2,
                        u'value': u'false'
                    },
                    u'hispanic':
                    {
                        u'confidence': 3,
                        u'value': u'false'
                    },
                    u'indian':
                    {
                        u'confidence': 3,
                        u'value': u'false'
                    },
                    u'white':
                    {
                        u'confidence': 85,
                        u'value': u'true'
                    }
                },
                u'eyes':
                {
                    u'confidence': 30,
                    u'value': u'open'
                },
                u'face':
                {
                    u'confidence': 95,
                    u'value': u'true'
                },
                u'fear':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'gender':
                {
                    u'confidence': 83,
                    u'value': u'male'
                },
                u'glasses':
                {
                    u'confidence': 26,
                    u'value': u'false'
                },
                u'happiness':
                {
                    u'confidence': 100,
                    u'value': u'true'
                },
                u'hat':
                {
                    u'confidence': 29,
                    u'value': u'false'
                },
                u'lips':
                {
                    u'confidence': 53,
                    u'value': u'parted'
                },
                u'liveness':
                {
                    u'confidence': 66,
                    u'value': u'true'
                },
                u'mood':
                {
                    u'confidence': 100,
                    u'value': u'happy'
                },
                u'neutral_mood':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'sadness':
                {
                    u'confidence': 0,
                    u'value': u'false'
                },
                u'smiling':
                {
                    u'confidence': 84,
                    u'value': u'true'
                },
                u'surprise':
                {
                    u'confidence': 0,
                    u'value': u'false'
                }
            },
            u'center':
            {
                u'x': 40.15,
                u'y': 38.93
            },
            u'confirmed': False,
            u'eye_left':
            {
                u'confidence': 98,
                u'id': 449,
                u'x': 44.89,
                u'y': 34.29
            },
            u'eye_right':
            {
                u'confidence': 98,
                u'id': 450,
                u'x': 36.16,
                u'y': 33.93
            },
            u'height': 39.64,
            u'label': None,
            u'manual': False,
            u'mouth_center':
            {
                u'confidence': 91,
                u'id': 615,
                u'x': 41.15,
                u'y': 47.14
            },
            u'nose':
            {
                u'confidence': 99,
                u'id': 403,
                u'x': 41.4,
                u'y': 40.71
            },
            u'pitch': -3,
            u'points': None,
            u'recognizable': True,
            u'similarities': None,
            u'tid': u'TEMP_F@01aa37a755638d436b6d527a00a1006d_ffc4e38fc72b2_40.15_38.93_0_1',
            u'uids': [],
            u'width': 19.7,
            u'yaw': -4
        }],
        u'url': u'http://api.skybiometry.com/fc/images/get?id=bmN2X3hybD1xd3RhY2FvcXJnYzFpMXdkMjIwNGZ4YXB6dCZuY3ZfZnJwZXJnPXZiM3liOXIyOXlmZ25lNjB0bjdvdzlycHRvJmVxPTI3NTMmY3ZxPXNzcDRyMzhzcDcybzImZ3Z6cmZnbnpjPTIwMjExMjE1MTUxNjMz',
        u'width': 401
    }], u'status': u'success', u'usage':
    {
        u'limit': 100,
        u'remaining': 80,
        u'reset_time': 1639553771,
        u'reset_time_text': u'Wed, 15 December 2021 07:36:11 +0000',
        u'used': 20
    }
}

Pretty-printing your data makes it a lot easier to analyze.漂亮地打印您的数据使分析变得更加容易。 Once you do that, as in my edit above, you'll see that you can access "mood" via data["photos"][0]["tags"][0]["attributes"]["mood"] , assuming your dict is named data .一旦你这样做了,就像我上面的编辑一样,你会看到你可以通过data["photos"][0]["tags"][0]["attributes"]["mood"]访问"mood" mood” ,假设您的 dict 名为data

You need the numeric list indices because both "photos" and "tags" are lists containing a single element - a dict.您需要数字列表索引,因为"photos""tags"都是包含单个元素的列表 - 字典。

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

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