我有以下的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)
一切都將呈現爲數組,這不是我在找什麼。
關於如何在數組必須被使用時如何解碼帶有數組的字符串以及何時可以避免數組的對象的任何想法?
試試這個吧? '$ icwsCallQueue - > {100259152} - > Eic_State'。另外,你可以通過索引訪問對象嗎? – Maximus2012
http://stackoverflow.com/questions/7027615/accessing-class-properties-with-spaces適用於任何「非法」密鑰名稱。 –
如果您不需要直接訪問'$ icwsCallQueue'屬性,則可以迭代。 – venca