简体   繁体   English

解析从 API 返回的 json

[英]Parse json returned from API

API returns following json. API 返回以下 json。

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

How can I access value for ResponseCode ?如何访问ResponseCode值?

Taking into account that the posted string is NOT valid JSON you can extract ResponseCode values using preg_match_all function:考虑到发布的字符串不是有效的 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);

The output:输出:

Array
(
    [0] => 200
    [1] => 400
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM