从API返回的解析json
问题描述:
API返回以下json。从API返回的解析json
$jsonData = '{"ResponseCode":200, "ResponseDetail":"Success", "AccessToken":"kksjfdlk"}{"ResponseCode":400, "ResponseDetail":"False"}';
如何获取ResponseCode
的值?
答
考虑到张贴的字符串不是有效的JSON可以提取使用preg_match_all
功能ResponseCode
值:
$jsonData = '{"ResponseCode":200, "ResponseDetail":"Success", "AccessToken":"kksjfdlk"}{"ResponseCode":400, "ResponseDetail":"False"}';
preg_match_all("/\"ResponseCode\"\s?:\s?(\d+)/", $jsonData, $m);
$response_codes = [];
// if there are matches
isset($m[1]) && $response_codes = $m[1];
print_r($response_codes);
输出:
Array
(
[0] => 200
[1] => 400
)
*过滤通过* - 你有什么过滤? – RomanPerekhrest
我想输出一个ResponseCode $ json = json_decode($ jsonData); $ json-> ResponseCode; –
问题太广泛,不清楚。 –