在PHP中阅读Json结果

时间:2018-04-24 10:02:59

标签: php soap

我有一个成功运行并返回json数据的服务器的soap请求。

{"GetAccountResult":{"Header":{"Rquid":"D560BC95-24F2-F705-0585-7CCB38E37ECE","Status":{"StatusCode":"-4","Message":"Account not found","Details":null}},"Account":null}}

我想知道如何在php中访问它并只打印消息?不是整个json的结果?

我试过了

$account   = $client->GetAccount($arrParams);
            $results = json_encode($account);

            echo $results->Message;

但是这仍然打印完整的json对象

4 个答案:

答案 0 :(得分:1)

以下是解决方案:

$account   = '{"GetAccountResult":{"Header":{"Rquid":"D560BC95-24F2-F705-0585-7CCB38E37ECE","Status":{"StatusCode":"-4","Message":"Account not found","Details":null}},"Account":null}}';
$results = json_decode($account,true);    // 2nd argument `true` returns array from JSON
echo $results['GetAccountResult']['Header']['Status']['Message'];

答案 1 :(得分:0)

$result = json_decode($your_json, true);

$message = $result["GetAccountResult"]["Header"]["Status"]["Message"];

我强烈建议使用isset()检查索引是否存在。否则你可能会出错。

答案 2 :(得分:0)

您可以通过json_decode检索数据:

$json = '{"GetAccountResult":{"Header":{"Rquid":"D560BC95-24F2-F705-0585-7CCB38E37ECE","Status":{"StatusCode":"-4","Message":"Account not found","Details":null}},"Account":null}}';
$t =  json_decode($json,true);
print_r($t['GetAccountResult']['Header']['Status']['Message']);

答案 3 :(得分:0)

检查此解决方案:

$account   = $client->GetAccount($arrParams);
$results   = json_decode($account);

echo $results->GetAccountResult->Header->Status->Message;