2015-06-24 44 views
0

我有以下的JSON字符串如何將json字符串解碼爲半個數組/半個對象?

$str = '{"icwsCallQueue":{"1002598152":{"AccoRDI_account_id":"","AccoRDI_mid":"","Eic_CallDirection":"O","Eic_CallIdKey":"100259815260150624","Eic_CallState":"Disconnected","Eic_ConferenceId":"","Eic_ConferenceMembers":"","Eic_ImmediateAccess":"1","Eic_LocalName":"Full Name","Eic_LocalUserId":"username","Eic_MonitorsCombinedCount":"0","Eic_Muted":"","Eic_ObjectType":"Call","Eic_ParentConferenceId":"","Eic_RemoteAddress":"307","Eic_RemoteName":"username-test","Eic_State":"I"},"1002598162":{"AccoRDI_account_id":"","AccoRDI_mid":"","Eic_CallDirection":"O","Eic_CallIdKey":"100259816260150624","Eic_CallState":"Disconnected","Eic_ConferenceId":"","Eic_ConferenceMembers":"","Eic_ImmediateAccess":"1","Eic_LocalName":"Full Name","Eic_LocalUserId":"username","Eic_MonitorsCombinedCount":"0","Eic_Muted":"","Eic_ObjectType":"Call","Eic_ParentConferenceId":"","Eic_RemoteAddress":"307","Eic_RemoteName":"username-test","Eic_State":"I"}}}'; 

如果我對其進行解碼使用json_decode($str) 我得到以下

stdClass Object 
(
    [icwsCallQueue] => stdClass Object 
     (
      [1002598152] => stdClass Object 
       (
        [AccoRDI_account_id] => 
        [AccoRDI_mid] => 
        [Eic_CallDirection] => O 
        [Eic_CallIdKey] => 100259815260150624 
        [Eic_CallState] => Disconnected 
        [Eic_ConferenceId] => 
        [Eic_ConferenceMembers] => 
        [Eic_ImmediateAccess] => 1 
        [Eic_LocalName] => Full Name 
        [Eic_LocalUserId] => username 
        [Eic_MonitorsCombinedCount] => 0 
        [Eic_Muted] => 
        [Eic_ObjectType] => Call 
        [Eic_ParentConferenceId] => 
        [Eic_RemoteAddress] => 307 
        [Eic_RemoteName] => username-test 
        [Eic_State] => I 
       ) 

      [1002598162] => stdClass Object 
       (
        [AccoRDI_account_id] => 
        [AccoRDI_mid] => 
        [Eic_CallDirection] => O 
        [Eic_CallIdKey] => 100259816260150624 
        [Eic_CallState] => Disconnected 
        [Eic_ConferenceId] => 
        [Eic_ConferenceMembers] => 
        [Eic_ImmediateAccess] => 1 
        [Eic_LocalName] => Full Name 
        [Eic_LocalUserId] => username 
        [Eic_MonitorsCombinedCount] => 0 
        [Eic_Muted] => 
        [Eic_ObjectType] => Call 
        [Eic_ParentConferenceId] => 
        [Eic_RemoteAddress] => 307 
        [Eic_RemoteName] => username-test 
        [Eic_State] => I 
       ) 

     ) 

) 

這樣做的問題是,我不能只用一個整數訪問屬性。我不能做到這一點$icwsCallQueue->100259152->Eic_State 所以我需要做一些如何我解碼字符串轉換爲這樣的事情

stdClass Object 
(
    [icwsCallQueue] => Array 
     (
      [1002598152] => stdClass Object 
       (
        [AccoRDI_account_id] => 
        [AccoRDI_mid] => 
        [Eic_CallDirection] => O 
        [Eic_CallIdKey] => 100259815260150624 
        [Eic_CallState] => Disconnected 
        [Eic_ConferenceId] => 
        [Eic_ConferenceMembers] => 
        [Eic_ImmediateAccess] => 1 
        [Eic_LocalName] => Full Name 
        [Eic_LocalUserId] => username 
        [Eic_MonitorsCombinedCount] => 0 
        [Eic_Muted] => 
        [Eic_ObjectType] => Call 
        [Eic_ParentConferenceId] => 
        [Eic_RemoteAddress] => 307 
        [Eic_RemoteName] => username-test 
        [Eic_State] => I 
       ) 

      [1002598162] => stdClass Object 
       (
        [AccoRDI_account_id] => 
        [AccoRDI_mid] => 
        [Eic_CallDirection] => O 
        [Eic_CallIdKey] => 100259816260150624 
        [Eic_CallState] => Disconnected 
        [Eic_ConferenceId] => 
        [Eic_ConferenceMembers] => 
        [Eic_ImmediateAccess] => 1 
        [Eic_LocalName] => Full Name 
        [Eic_LocalUserId] => username 
        [Eic_MonitorsCombinedCount] => 0 
        [Eic_Muted] => 
        [Eic_ObjectType] => Call 
        [Eic_ParentConferenceId] => 
        [Eic_RemoteAddress] => 307 
        [Eic_RemoteName] => username-test 
        [Eic_State] => I 
       ) 

     ) 

) 

這樣我就可以訪問另一方面,如果像這樣$icwsCallQueue['100259152']->Eic_State

記錄我解碼字符串像這樣json_decode($str, true)一切都將呈現爲數組,這不是我在找什麼。

關於如何在數組必須被使用時如何解碼帶有數組的字符串以及何時可以避免數組的對象的任何想法?

+0

試試這個吧? '$ icwsCallQueue - > {100259152} - > Eic_State'。另外,你可以通過索引訪問對象嗎? – Maximus2012

+0

http://stackoverflow.com/questions/7027615/accessing-class-properties-with-spaces適用於任何「非法」密鑰名稱。 –

+0

如果您不需要直接訪問'$ icwsCallQueue'屬性,則可以迭代。 – venca

回答

0

您可以在需要時將數組強制轉換爲stdclass和stdclass數組。

首先得到json_decode($str, true)的關聯版本的數據。然後遍歷數據的最內部部分並將數組再次轉換回對象。現在可以使用stdclass屬性作爲訪問器。

$str = '{"icwsCallQueue":{"1002598152":{"AccoRDI_account_id":"","AccoRDI_mid":"","Eic_CallDirection":"O","Eic_CallIdKey":"100259815260150624","Eic_CallState":"Disconnected","Eic_ConferenceId":"","Eic_ConferenceMembers":"","Eic_ImmediateAccess":"1","Eic_LocalName":"Full Name","Eic_LocalUserId":"username","Eic_MonitorsCombinedCount":"0","Eic_Muted":"","Eic_ObjectType":"Call","Eic_ParentConferenceId":"","Eic_RemoteAddress":"307","Eic_RemoteName":"username-test","Eic_State":"I"},"1002598162":{"AccoRDI_account_id":"","AccoRDI_mid":"","Eic_CallDirection":"O","Eic_CallIdKey":"100259816260150624","Eic_CallState":"Disconnected","Eic_ConferenceId":"","Eic_ConferenceMembers":"","Eic_ImmediateAccess":"1","Eic_LocalName":"Full Name","Eic_LocalUserId":"username","Eic_MonitorsCombinedCount":"0","Eic_Muted":"","Eic_ObjectType":"Call","Eic_ParentConferenceId":"","Eic_RemoteAddress":"307","Eic_RemoteName":"username-test","Eic_State":"I"}}}'; 
    $json = json_decode($str, true); 
    foreach($json["icwsCallQueue"] as $key => $element) { 
    $json["icwsCallQueue"][$key] = (object)$element; 
    } 
    var_dump($json["icwsCallQueue"]['1002598152']->Eic_CallIdKey); 
    var_dump($json["icwsCallQueue"]['1002598162']->Eic_CallIdKey); 

還有另一個更簡單的解決方案,但我無法使它工作。這就是爲什麼我必須迭代所有數組。

$str = '{"icwsCallQueue":{"1002598152":{"AccoRDI_account_id":"","AccoRDI_mid":"","Eic_CallDirection":"O","Eic_CallIdKey":"100259815260150624","Eic_CallState":"Disconnected","Eic_ConferenceId":"","Eic_ConferenceMembers":"","Eic_ImmediateAccess":"1","Eic_LocalName":"Full Name","Eic_LocalUserId":"username","Eic_MonitorsCombinedCount":"0","Eic_Muted":"","Eic_ObjectType":"Call","Eic_ParentConferenceId":"","Eic_RemoteAddress":"307","Eic_RemoteName":"username-test","Eic_State":"I"},"1002598162":{"AccoRDI_account_id":"","AccoRDI_mid":"","Eic_CallDirection":"O","Eic_CallIdKey":"100259816260150624","Eic_CallState":"Disconnected","Eic_ConferenceId":"","Eic_ConferenceMembers":"","Eic_ImmediateAccess":"1","Eic_LocalName":"Full Name","Eic_LocalUserId":"username","Eic_MonitorsCombinedCount":"0","Eic_Muted":"","Eic_ObjectType":"Call","Eic_ParentConferenceId":"","Eic_RemoteAddress":"307","Eic_RemoteName":"username-test","Eic_State":"I"}}}'; 
    $json = json_decode($str); 
    $jsonArray = (array)$json->icwsCallQueue; 
    var_dump($jsonArray["1002598152"]); 
    //PHP Notice: Undefined offset: 1002598152 in 

看起來像PHP不會用整數鍵正確地轉換爲數組。或者我在這裏錯過了一些東西。

其餘的是你的想象力。

相關問題