【问题标题】:ErrorException in dataController.php line 98: Undefined property: stdClass::$id LaraveldataController.php 第 98 行中的 ErrorException:未定义属性:stdClass::$id Laravel
【发布时间】:2017-01-02 08:12:13
【问题描述】:

我正在尝试从 JSON 请求中获取“id”值。但我收到此错误未定义属性:stdClass::$id Laravel。

$data = json_decode($request->getContent());
foreach ($data->data as $data){
            $id = $data->id;


  $modsyPrice = floor($data->modsyPrice);
}

这是我的 Json:

{"data":{"0":{"ID":"1","Name":"3 Piece Roundabout Candle Holders","Url": 

"http://url","ModsyStatus":"0","weRplayStatus"

:"0", "ModsyPrice" :"39.95", "weRplayPrice":"39.95","activeLoader":true} }}

【问题讨论】:

  • print_r($data)开头
  • [data] => stdClass 对象([0] => stdClass 对象([ID] => 1 [Name] => 3 件环岛烛台)
  • 同样的错误 :( .
  • 现在想想你的代码有什么问题。
  • @waleed bin khalid 试试下面的代码它正在工作。

标签: php json laravel


【解决方案1】:

ID 大写:$id = $data->ID;

同理:ModsyPrice

【讨论】:

    【解决方案2】:

    尝试如下:你的数组是多维的。

    /*$json = '{"data":{"0":{"ID":"1","Name":"3 Piece Roundabout Candle Holders","Url": 
    
    "http://url","ModsyStatus":"0","weRplayStatus"
    
    :"0", "ModsyPrice" :"39.95", "weRplayPrice":"39.95","activeLoader":true} }}';*/
    
    
    $json =json_decode($request->getContent(),true); //get your json
    $array = json_decode($json,true);
    //print_r($array);
        foreach ($array['data'] as $data){
                    $id = $data['ID'];
                    $modsyPrice = floor($data['ModsyPrice']);
        }
    echo $id."<br/>";
    echo $modsyPrice;
    

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多