【问题标题】:Parsing JSON Feed解析 JSON 提要
【发布时间】: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


【解决方案1】:
$my_array_for_parsing = json_decode(/** put the json here */);

这为您提供了作为 php associative 数组的 JSon。


$my_array_for_parsing = json_decode($json);
foreach ($my_array_for_parsing as $name => $value) {
    // This will loop three times:
    //     $name = a
    //     $name = b
    //     $name = c
    // ...with $value as the value of that property
}

【讨论】:

  • 我哪里没有?在php中,你没有JSON,你只能得到一个关联数组json_decode进行转换。 JSON 是一个javascript 对象。 “php json”是一个关联数组。
  • 关于 foreach 输出的部分不正确。 OP 返回一个带有 JSON 的数组,而不是他试图输出的对象。
  • 我发布了一个新答案。工作
【解决方案2】:

如果json_decode 的第二个参数设置为true,您的json 将转换为关联数组而不是对象。试试这个:

$obj = json_decode($json, false);

foreach($obj as $ob) {
    echo $ob->ID;
}   

【讨论】:

  • 没有帮助警告:为 foreach() 提供的参数无效
  • 确保$obj 不为空。 var_dump( $obj );的输出是什么
  • string(668) "[{"ID":1123,"OrderNumber":"1394","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL": "someone.biz/Home/ShowTemplate/283","ShipDate":"2/28/2015","InHomeDate":"3/2/2015","数量":"10,000","价格":"$3,000 ","CallTracking":"0"},{"ID":1123,"OrderNumber":"1413","ProjectType":"Postcard","Template":"WtlossStudy solo","TemplateURL":"", "ShipDate":"3/30/2015","InHomeDate":"3/31/2015","数量":"5,000","价格":"$1,500","CallTracking":"0"}]"
  • 你能在你的问题中粘贴整个代码(更新)吗?
【解决方案3】:

试一试

$json = file_get_contents('http://superiorpostcards.biz/api/api/1123');
$obj = json_decode($json, true);
$array = json_decode($obj, true);
foreach($array as $value){
    echo $value['ID'];
}

【讨论】:

  • 即使在第一个 json_decode 之后,字符串也是 JSON 格式,这就是为什么 @PHPhil
【解决方案4】:

这行得通。

由于您的 JSON 已成为关联数组,因此您必须创建 2 个 foreach。

  • top foreach 解析 '[object1, object2, object3]' 中的 3 个“对象”
  • 底部foreach解析每个“对象”内容

    $data = json_decode('[{"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"}]');
    
     foreach($data as $obj) {
         foreach($obj as $key=>$val) {
            echo $key."->".$val." | ";
         }
     }   
    

是的,JS 更简单。但是php“json”并不是一个JS对象,它是一个关联数组的数组。

【讨论】:

  • 感谢您的努力。当我使用 json 提要而不是 url 但我想使用 URL 时,它可以工作。已经得到解决方案。再次感谢你:)
  • 欢迎,享受编码。如果我的答案正确,请给我 +1 和支票。这将推动读者。干杯(顺便说一句,访问斯里兰卡的最佳时间是什么时候?)
猜你喜欢
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
相关资源
最近更新 更多