【问题标题】:Parse String of objects of stdClass in php在php中解析stdClass对象的字符串
【发布时间】:2019-06-01 11:24:23
【问题描述】:

我有一个 stdClass 对象字符串数组,但我无法正确解析字符串

我尝试了各种方法,但都给出了“获取非对象的属性”的相同错误

foreach($response->Product->$x as $prod)
{
print $prod->Name;
print $prod->Price;
print $prod->Stock;
print $prod->SupplierName;
print $prod->CategoryName;
$x++;
}

//但是我的响应字符串是

stdClass Object ( 
[Product] => Array 
( [0] => stdClass Object
( [Id] => 2 [Name] => Green Shirt [Price] => 1800 [Stock] => 50 [SupplierName] => Ali Yar [CategoryName] => Jeans )
  [1] => stdClass Object
  ( [Id] => 7 [Name] => Red Shirt with Flowers [Price] => 2200 [Stock] => 40 [SupplierName] => Ali Yar [CategoryName] => Shirt )
  [2] => stdClass Object
  ( [Id] => 8 [Name] => Red Shirt with Flowers [Price] => 2200 [Stock] => 40 [SupplierName] => Ali Yar [CategoryName] => Shirt ) 
  [3] => stdClass Object
  ( [Id] => 9 [Name] => Check Shirt Yellow [Price] => 1100 [Stock] => 200 [SupplierName] => Ali Yar [CategoryName] => Shirt 
  )
 )
 ) 

我收到这些错误尝试获取为 foreach() 提供的非对象和无效参数的属性

【问题讨论】:

  • 什么是$response->Product->$x?
  • $response 是我来自 java webservice 的响应即将到来的变量...您可以看到的产品是我响应中的 stdClass 对象...
  • 如果[Product] 是 Array 那么使用-> 是不正确的。

标签: php string parsing stdclass


【解决方案1】:

由于Product属性是数组,那么:

foreach($response->Product as $prod)
{

    print $prod->Name;
    print $prod->Price;
    print $prod->Stock;
    print $prod->SupplierName;
    print $prod->CategoryName;

    // this increment is useless
    //$x++;

}

【讨论】:

    【解决方案2】:

    您的代码不清楚,但我认为您必须在其中编辑foreach

    foreach($response->Product as $prod)
    {
    print $prod->Name;
    print $prod->Price;
    print $prod->Stock;
    print $prod->SupplierName;
    print $prod->CategoryName;
    $x++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 2017-06-21
      • 2012-01-07
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 2012-11-16
      相关资源
      最近更新 更多