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条)

报告相同问题?

悬赏问题

  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要