未定义的索引:faceId in <b>/storage/ssd1/900/12273900/public_html/upload_image.php</b> on line <b>30

Undefined index: faceId in <b>/storage/ssd1/900/12273900/public_html/upload_image.php</b> on line <b>30

我正在尝试解析 JSON 并使用 PHP 从中提取一个名为 faceId 的特定密钥。但是,当我尝试解析它时,我遇到了以下错误。

<b>Notice</b>:  Undefined index: faceId in <b>/storage/ssd1/900/12273900/public_html/upload_image.php</b> on line <b>30</b><br />
I/flutter (18628): <br />

这是我的 JSON

[
{
    "faceId": "a1e0ee95-3365-40b0-91f2-e0a05bdeadcc",
    "faceRectangle": {
        "top": 158,
        "left": 298,
        "width": 226,
        "height": 226
    },
    "faceAttributes": {
        "age": 19.0
    }
}]

这就是我尝试解析它的方式,但它总是会引发错误。

$data = array('image' => $imageUrl);


$options = array(
'http' => array(
'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
'method'  => 'POST',
'content' => http_build_query($data),
),
);

$context  = stream_context_create($options);

$result1 = file_get_contents($url, true, $context);



$jsonFaceID = json_decode($result1,true);
$finalFaceID= $jsonFaceID['faceId'];//the error appears here.

我也试过其他方法,还是一样的错误。

$jsonFaceID = json_decode($result1,false);//it returns a JSON object
$finalFaceID= $jsonFaceID->faceId;

我无法理解错误背后的逻辑。任何帮助将非常感激。非常感谢

我通过 MS Face API 对 detect_face.

的 POST 请求获得结果

那json是数组中的一个对象

Array
(
    [0] => stdClass Object
        (
            [faceId] => a1e0ee95-3365-40b0-91f2-e0a05bdeadcc
            [faceRectangle] => stdClass Object
                (
                    [top] => 158
                    [left] => 298
                    [width] => 226
                    [height] => 226
                )

            [faceAttributes] => stdClass Object
                (
                    [age] => 19
                )
        )
)

所以代码应该是

$finalFaceID= $jsonFaceID[0]->faceId;