2016-07-08 85 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

不客氣:) –