How to access a member of a class which is inside another object from JSON using PHP

后端 未结 3 1960
孤城傲影
孤城傲影 2021-01-27 01:22

I have a JSON String like this

$test=\'{\"var1\":null,\"var3\":null,\"status\":{\"code\":150,\"message\":\"blah blah\"}}\';

I want to access th

相关标签:
3条回答
  • 2021-01-27 01:50

    Not sure what you're jsonService is doing but this worked for me:

    $json = '{"var1":null,"var3":null,"status":{"code":150,"message":"blah blah"}}';
    
    $result = json_decode($json);
    
    echo $result->status->code;
    
    0 讨论(0)
  • 2021-01-27 01:57

    You can use json_decode() for this task. Also, your input string should have quotes:

    $test='{"var1":null,"var3":null,"status":{"code":150,"message":"blah blah"}}';
    
    $responseObj = json_decode($test);
    
    echo $responseObj->status->code;
    
    0 讨论(0)
  • 2021-01-27 02:14

    You should give PHP's json_decode() a try:

    $test='{"var1":null,"var3":null,"status":{"code":150,"message":"blah blah"}}';
    $responseObj = json_decode($test);
    echo $responseObj->status->code;
    

    For PEARS's Services_JSON Class (Documentation):

    // create a new instance of Services_JSON
    $jsonService = new Services_JSON();
    
    $test='{"var1":null,"var3":null,"status":{"code":150,"message":"blah blah"}}';
    $jsonService->decode($test);
    echo $responseObj->status->code;
    
    0 讨论(0)
提交回复
热议问题