2016-10-07 144 views
-2

我正在从Web服务接收下面的输出。 (图中的输出是通过var_dump(json_decode($json,true));要从对象本人可以使用的后半部分获得的数据,例如:需要语法才能访问json对象中的数组

$obj = json_decode($json); 
$myvalue=$obj->{'CurrentAccountNumber'}; 

但我还需要在对象的开始到得到来自阵列数据(通过PHP) ?什么是正确的语法使用使用PHP来获取/回波的阵列数据谢谢您从WEB SERVICE

OUTPUT:

阵列(30){[ “帐户”] =>阵列( 2){[0] => array(4){[“AccountNumber”] => string(9)“123456789”[“CurrentBalance”] => float(455.17)[“FullName”] => string(8) JOHN DOE“[”Code“) (3)“A34”} [1] => array(4){[“AccountNumber”] => string(9)“123456788”[“CurrentBalance”] => float(67.22)[“ FullName“] => string(8)”JOHN DOE“[”CodeNum“] => string(3)”B82“}}

如何从此行的数组中获取数据?

[ “CurrentAccountNumber”] =>串(9) “123456789”[ “CurrentBalance”] =>浮动(455.17)[ “CustomMonth”] => BOOL(真)[ “CustomMonthAmt”] =>浮(488.17)[“CustomMonthLength”] => int(0)[“DateOfService”] => string(10)“2013-06-15”[“十八月”] => bool(false)[“十八月月”] => int(0)[“ExpectedLastPaymentAmount”] => float(0)[“ExpectedLastPaymentDate”] => string(0)“”[“FullName”] => string(8)“JOHN DOE”[“Email”] => string(11)“myemail.com”[“LastFourPhone”] => string(4)“5128”[“LastPaymentAmount”] => float(4205.83)[“LastPaymentDate”] => string(10) [“NextPaymentAmount”] => float(0)[“NextPaymentDate”] => string(0)“”[“Category”] => int(0)[“InsPlanStartDate”] => string(10) “08-08”[“PrimaryPhoneNumber”] => string(10)“5555555128”[“SixMonth”] => bool(false)[“SixMonthAmt”] => int(0)[“CodeNum”] => strin g(3)“A34”[“TwelveMonth”] => bool(false)[“TwelveMonthAmt”] => int(0)[“TwentyFourMonth”] => bool(false)[“TwentyFourMonthAmt”] => int(0 )[ “UseInfoOnFile”] => BOOL(真)[ “邮编”] =>串(5) “12345”}

+0

首先将你的json数据解码为一个数组,然后将其解码为一个对象。你也向我们展示了来自同一个变量的两个不同的数据输出。所以你在这里混合了一些东西。 – Rizier123

+0

是的,json对象返回的是对象第一部分的数组和第二部分的字符串。谢谢您的回复。 – RWilliams

回答

0

使用$obj->CurrentAccountNumber语法来代替。

如果您将json解码为数组而不是对象,则可以将其与典型的数组语法$obj['CurrentAccountNumber']一起使用,就像您在var_dump中所做的那样。

要做到这一点改变这个

$obj = json_decode($json); 

$array = json_decode($json, true); 
+0

谢谢你的回复。 – RWilliams

0

json_decode($json)使得StdClass对象了JSON数据(如果您没有设置第二个参数来true,看http://php.net/manual/en/function.json-decode.php ),所以你可以通过$obj->propertyName访问它的属性,如果它是一个数组(使用foreach左右)迭代其他属性。

如果您想保留一个关联数组,请保留第二个参数json_decode($json, true);并像其他关联数组一样访问值(但这里的变量名称很混乱)。

+0

谢谢你的回复。使用foreach工作。 – RWilliams