dougao9864 2014-07-26 07:33
浏览 61
已采纳

注意:尝试获取非对象的属性 - 也许是字符串?

I would like to get the value of "Records"

When is try this code:

$json = file_get_contents('URL');
var_dump($json);

This is the result:

string(25289) "{ "ProductsSummary": { "Records": 10, "TotalRecords": 2874, "TotalPages": 288, "CurrentPage": 1 }, "Products": [ { "...

When I try this code

$json = file_get_contents('URL');
$obj = json_decode($json);

echo $obj;
echo $obj->{'ProductsSummary'}->{'Records'};
echo $obj->ProductsSummary->Records;
echo $obj[0]->ProductsSummary->Records;
echo $obj->ProductsSummary[0]->Records;
echo $obj->ProductsSummary[1];

The Output is:

{ "ProductsSummary": { "Records": 10, "TotalRecords": 2879, "TotalPages": 288, "CurrentPage": 1 }, "Products": [ { "Last.... }

Notice: Trying to get property of non-object in  ...
Notice: Trying to get property of non-object in  ...
Notice: Trying to get property of non-object in  ...
  • 写回答

2条回答 默认 最新

  • duanji4449 2014-07-26 10:48
    关注

    Since you do not have valid JSON, I'm pretty sure that json_decode() is just failing. As per the manual:

    Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

    You can verify that with e.g. the is_null() function:

    $obj = json_decode($json);
    if( is_null($obj) ){
        // Invalid JSON, don't need to keep on working on it
    }else{
        // Read data
    }
    

    If everything works fine, $obj will be an object, thus feeding echo with it will never yield anything useful:

    echo $obj;
    

    You might want to use var_dump() instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line