无法使用json_decode()解析JSON web服务响应


Unable to parse JSON web service response with json_decode()

在服务返回错误的情况下,我正在努力解析web服务响应JSON。

示例JSON -成功流程:

{
    "Response": [{
        "iconPath" : "/img/theme/destiny/icons/icon_psn.png",
        "membershipType": 2, 
        "membershipId": "4611686018429261138",    
        "displayName": "Spuff_Monkey"
     }],
    "ErrorCode": 1,
    "ThrottleSeconds": 0,
    "ErrorStatus": "Success",
    "Message": "Ok",
    "MessageData":{}
}

示例JSON -错误流程:

{
    "ErrorCode": 7,
    "ThrottleSeconds": 0,
    "ErrorStatus": "ParameterParseFailure",
    "Message": "Unable to parse your parameters.  Please correct them, and try again.",
    "MessageData": {}
}

现在我的PHP:

function hitWebservice($endpoint) {
    $curl = curl_init($endpoint);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
    $json_response = curl_exec($curl);
    if(curl_exec($curl) === false) {
        echo "Curl error: " . curl_error($curl);
    }
    curl_close($curl);
    $array_response = json_decode($json_response, true);
    $function_response = array();
    if (!isset($array_response['Response'])) {
        $function_response = $array_response;
    } else {
        $function_response = $array_response['Response'];
    }
    return $function_response;
}

我想要实现的是,当JSON包含"响应"块时,我把它放在一个新的数组中,只返回函数的细节,其中"响应"不存在,我想返回完整的JSON作为一个数组。

然而,目前,没有"响应",我得到一个空数组。

我的逻辑出了问题,在我小小的头脑里,我无法克服它,所以是时候寻求帮助了!

Response是JSON中的对象数组这一事实判断,我怀疑错误流响应也可能包含Response字段,但以空数组作为值([])。这就解释了你现在的结果。

不检查Response是否存在。它可能只是一个空数组。相反,检查ErrorCodeErrorStatusErrorMessage(您认为最合适的哪个)。例如:

if ($array_response['ErrorStatus'] != "Success") {
    $function_response = $array_response;
} else {
    if (!isset($array_response['Response'])) {
        $function_response = null;
    } else {
        $function_response = $array_response['Response'];
    }
}

Success -的情况下,您希望检查Response是否存在,以便如果它不存在(但是期望的),您可以引发一些错误)。

另一个可能的解决方案是计算响应的数量:

if (!isset($array_response['Response'])) {
    $function_response = $array_response;
} else {
    if (count($array_response['Response']) > 0) {
        $function_response = $array_response['Response'];
    } else {
        $function_response = $array_response;
    }
}

如果您注意到,好的和坏的响应都包含一个ErrorCode

你最好设计你的代码从这个字段工作,而不是测试一个可能存在也可能不存在的字段。

那么试试这个:-

$array_response = json_decode($json_response, true);
switch ( $array_response['ErrorCode'] ) {
case 1 :
    do_errorCode_1_processing($array_response)
    break;
 case 2 :
    do_errorCode_2_processing($array_response)
    break;
 // etc etc

}

isset()不是检查数组中键是否存在的正确函数。

请使用array_key_exists()。

http://php.net/manual/en/function.array-key-exists.php

所以你的代码应该是这样的:
$array_response = json_decode($json_response, true);
$function_response = array();
if (array_key_exists('Response', $array_response)) {
    $function_response = $array_response['Response'];
} else {
    $function_response = $array_response;
}
return $function_response;