Parse json从API返回

时间:2017-02-11 14:23:11

标签: php

API返回json。

$jsonData = '{"ResponseCode":200, "ResponseDetail":"Success", "AccessToken":"kksjfdlk"}{"ResponseCode":400, "ResponseDetail":"False"}';

如何访问ResponseCode的价值?

1 个答案:

答案 0 :(得分:0)

考虑到发布的字符串无效JSON,您可以使用ResponseCode函数提取preg_match_all值:

$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
)