我有以下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)
,一切都将呈现为数组,而不是我要找的。 p>
关于如何在必须使用数组时使用数组解码字符串以及何时可以避免数组的对象的任何想法?
答案 0 :(得分: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没有使用整数键正确地转换为数组。或者我在这里缺少一些东西。
其余的取决于你的想象力。