如何在变量中存储json响应

2024-06-28 20:45:24 发布

您现在位置:Python中文网/ 问答频道 /正文

params = {                                                                      #API parameters
        'returnFaceId': 'false',
        'returnFaceLandmarks': 'false',
        'returnFaceAttributes': 'smile',
    }

    response = requests.post(face_api_url, params=params,
                             headers=headers, json={"url": image_url})
    print(json.dumps(response.json()))

输出: [{“faceRectangle”:{“top”:125,“left”:154,“width”:94,“height”:94},“faceAttributes”:{“smile”:1.0}]

我想访问“微笑”属性并将其存储到变量中。我该怎么做


Tags: apijsonfalseurlresponseparamspostrequests
2条回答

这就是在本例中访问它的方式。 返回类型是list of dictionaries

>>> response[0]['faceAttributes']['smile']
>>> 1.0

您可以像这样访问smile,无需使用json.dumps。这是基于假设它返回数组?您确定它返回数组吗

用于阵列响应

      response.json[0]['faceAttributes']['smile']

对于字典响应

    response.json['faceAttributes']['smile']

相关问题 更多 >