-1

我有以下代码:

$url = "http://myserver/myapp/restapi.php?method=get_list";
$jsondata = curl($url);
print_r($jsondata);
$arrayObjects = json_decode($jsondata, true);
print_r($arrayObjects);

$jsondata 的打印工作......但 arrayObjects 不打印任何内容。我只包含了正确转储的 json 数据的第一个“记录”:

{"263":{"realm":"object","etags":
 [],"id":"263","name":"ddd","label":"","asset_no":null,"objtype_id":"4",
 "r_id":null,"R_name":null,"row_id":null,"Row_name":null,"container_id":null,
 "container_name":null,"has_problems":"no","comment":null,"nports":"3",
 "runs8021Q":"1","itags":[],"dname":"ddd","atags":[{"tag":"$id_263"},
{"tag":"$typeid_4"},{"tag":"$any_object"},{"tag":"$cn_ddd"},{"tag":"$unmounted"},  
 {"tag":"$no_asset_tag"},{"tag":"$runs_8Q"},{"tag":"$untagged"}]}
 },

我也没有在屏幕上显示任何错误消息。我试图解决它的方法是创建一个新的 php 文件,其中包含这个 json 数据作为字符串......然后我调用了 json decode。它工作正常。查看下面的代码:

  <?php

   $jsondata='{"263":{"realm":"object","etags":  
   [],"id":"263","name":"ddd","label":"","asset_no":null,
   "objtype_id":"4","r_id":null,"R_name":null,"row_id":null,
   "Row_name":null,"container_id":null,"container_name":null,
    "has_problems":"no","comment":null,"nports":"3","runs8021Q":"1","itags":     
    [],"dname":"ddd","atags":[{"tag":"$id_263"},
   {"tag":"$typeid_4"},
   {"tag":"$any_object"},{"tag":"$cn_ddd"},{"tag":"$unmounted"},  
   {"tag":"$no_asset_tag"},{"tag":"$runs_8021Q"},{"tag":"$untagged"}]},"187":
   {"realm":"object","etags":[],"id":"187","name":"a-ds-NSM-1",
   "label":"","asset_no":null,"objtype_id":"4","r_id":null,
   "R_name":null,"row_id":null,"Row_name":null,"container_id":null,
   "container_name":null,"has_problems":"no","comment":null,
   "nports":"3","runs8021Q":null,"itags":[],
   "dname":"ss-a-NSM-1","atags":[{"tag":"$id_187"},{"tag":"$typeid_4"},    
   {"tag":"$any_object"},{"tag":"$cn_ss-ds-1"},{"tag":"$unmounted"},   
   {"tag":"$no_asset_tag"},{"tag":"$untagged"}]},
   "123":{"realm":"object","etags":
    [],"id":"123","name":"df","label":"","asset_no":null,
    "objtype_id":"4","r_id":null,"R_name":null,"row_id":null,
   "Row_name":null,"container_id":null,"container_name":null,
   "has_problems":"no","comment":null,"nports":"3",
     "runs8021Q":null,"itags":[],"dname":"df",
    "atags":[{"tag":"$id_123"},{"tag":"$typeid_4"},
      {"tag":"$any_object"},{"tag":"$cn_df"},
   {"tag":"$unmounted"},{"tag":"$no_asset_tag"},{"tag":"$untagged"}]}}';


 print_r(json_decode($jsondata,true));
 ?>

任何关于我的代码有什么问题的想法都将不胜感激。

4

1 回答 1

0

$jsondata 的打印工作......但 arrayObjects 不打印任何内容。

这可能意味着 JSON 字符串无效。确保您有 error_reporting 和 display_errors = ON 以便您可以看到错误。您还可以检查您的 PHP 错误日志中的问题。

于 2012-08-10T18:57:50.457 回答