【发布时间】:2015-10-23 04:16:52
【问题描述】:
我在包含以下数据的 URL 中有一个 json 提要。
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
[{"ID":1123,"OrderNumber":"1394","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"someone.biz/Home/ShowTemplate/283","ShipDate":"2/28/2015","InHomeDate":"3/2/2015","Quantity":"10,000","Price":"$3,000","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"","ShipDate":"3/30/2015","InHomeDate":"3/31/2015","Quantity":"5,000","Price":"$1,500","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"","ShipDate":"4/13/2015","InHomeDate":"4/14/2015","Quantity":"5,000","Price":"$1,500","CallTracking":"0"}]
</string>
我需要得到它并通过 php.ini 解析它。但它使用以下代码给出了无效的 foreach 错误。任何人都可以帮助我如何正确显示。
$json = file_get_contents('http://someurl.biz/api/api/1123');
$obj = json_decode($json, true);
foreach($obj as $ob) {
echo $ob->ID;
}
【问题讨论】:
-
由于您在
json_decode()中使用true标志,您应该回显数组项,而不是对象。
标签: php json parsing string-parsing php-parser