2016-07-08 84 views
-1

从服务请求中,我得到以下响应,我尝试使用json_decode解析它,但它不起作用。我需要检查 ["body"]->enquiry->status是否“打开”。任何人都可以告诉我如何解析这个回应?使用PHP解析JSON数组不起作用

array(5) { 
    ["headers"] => array(5) { 
     ["server"] => string(17) 
     "Apache-Coyote/1.1" ["content-type"] => string(16) 
     "application/json" ["content-length"] => string(3) 
     "313" ["date"] => string(29) 
     "Fri, 08 Jul 2016 00:22:29 GMT" ["connection"] => string(5) 
     "close" 
    } 
    ["body"] => string(313){ 
     "version ":{ 
      "major ":1, 
      "minor ":6, 
      "revision ":0 
     }, 
     "enquiry ":{ 
      "id ":"21a2a688-c09b-48bc-8cb0-0ad596c18447", 
      "creationTime ":1467937344745, 
      "lastUpdateTime ":1467937344753, 
      "status ":"OPEN ", 
      "from ":"test ", 
      "email ":"[email protected] ", 
      "message ":"test ", 
     }, 
     "enquiries":null 
    } 
    ["response"] => array(2) { 
     ["code"] => int(202) 
     ["message"] => string(8) 
     "Accepted" 
    } 
    ["cookies"] => array(0) {} 
    ["filename"] => NULL 
} 
+1

刚刚访问相应的索引,JSON字符串与阵列标志进行解码,然后把它像任何正常阵列 – Ghost

+0

上面的代码是响应或与json_decode响应解码?我想看到原始的回应。 –

+0

这是我从服务中获得的原始响应。我实际上是PHP新手,非常感谢任何帮助。 – user3736514

回答

1

以下是如何访问它。我用$response作为你给我们的回应。

//We decode the 'body' from the response to json and we convert it to an array 
$body = json_decode($response['body'],true); 

//We access the status 
$status = $body['enquiry']['status']; 

另外,如果您想检查状态是否等于'OPEN',请小心。在你的回复中,状态是真实的OPEN。注意尾部空间。您可以使用trim($status)删除空格。

+0

非常感谢,我现在能够获得这个地位。 – user3736514

+0

好的,我会再次感谢你。 – user3736514

+0

不客气:) –