我有一个响应数据,比如
$response_array= "{status: 'false',description: '5100|4567897845'}"
json验证器表示它是一个有效的json。
但是当我尝试访问状态参数时
echo $response_array->status;
它什么也不输出。
如何获取状态值?
您的JSON字符串中有错误的引号,请尝试将字符串编辑为:
$response_array= '{"status": "false","description": "5100|4567897845"}';
然后你可以使用json_decode()
函数,比如:
$response_array = json_decode('{"status": "false","description": "5100|4567897845"}');
echo $response_array->status;
请使用json_decode,它用于解码json格式的字符串。因此,JSON对中的字符串元素最好始终包含在双qoute中。因此,请按上述格式格式化响应数组。然后,您可以简单地使用json_decode并访问各自的键值对。
$response_array= '{"status": "false","description": "5100|4567897845"}';
$obj = json_decode( $response_array);
echo $obj->description;
希望这能有所帮助。
对于php,返回json值是一个简单的字符串,您需要对其进行解码使用
json_decode
$response = json_decode($response_array);
如果它作为一个对象返回,您可以使用
$response->status
访问,或者如果它是一个数组,那么您可以像$response['status']
一样访问
为了将其作为数组返回,json_decode中的第二个参数必须设置为true。
$response = json_decode($response_array, true);