如何从stdClass中获取JSON项

时间:2014-04-03 00:34:55

标签: php json amazon

我正在使用Amazon API来提取产品信息。我用它来提取信息:

$response = $amazonEcs->responseGroup('OfferFull')->lookup('B00IXO8I0O');
var_dump($response);

它输出(警告 - 长输出,你可以滚动到底部):

我无法弄清楚如何获得" FormattedPrice。" (或任何相关项目)

如何开始从中取出数据而不是var_dump()。

object(stdClass)#6 (2) {
  ["OperationRequest"]=>
  object(stdClass)#7 (4) {
    ["HTTPHeaders"]=>
    object(stdClass)#8 (1) {
      ["Header"]=>
      object(stdClass)#9 (2) {
        ["Name"]=>
        string(9) "UserAgent"
        ["Value"]=>
        string(14) "PHP-SOAP/5.5.3"
      }
    }
    ["RequestId"]=>
    string(36) "3f1671a9-2b85-4905-9362-3f43f5652528"
    ["Arguments"]=>
    object(stdClass)#10 (1) {
      ["Argument"]=>
      object(stdClass)#11 (2) {
        ["Name"]=>
        string(7) "Service"
        ["Value"]=>
        string(19) "AWSECommerceService"
      }
    }
    ["RequestProcessingTime"]=>
    float(0.015202)
  }
  ["Items"]=>
  object(stdClass)#12 (2) {
    ["Request"]=>
    object(stdClass)#13 (2) {
      ["IsValid"]=>
      string(4) "True"
      ["ItemLookupRequest"]=>
      object(stdClass)#14 (4) {
        ["IdType"]=>
        string(4) "ASIN"
        ["ItemId"]=>
        string(10) "B00IXO8I0O"
        ["ResponseGroup"]=>
        string(9) "OfferFull"
        ["VariationPage"]=>
        string(3) "All"
      }
    }
    ["Item"]=>
    object(stdClass)#15 (3) {
      ["ASIN"]=>
      string(10) "B00IXO8I0O"
      ["OfferSummary"]=>
      object(stdClass)#16 (5) {
        ["LowestNewPrice"]=>
        object(stdClass)#17 (3) {
          ["Amount"]=>
          int(29999)
          ["CurrencyCode"]=>
          string(3) "USD"
          ["FormattedPrice"]=>
          string(7) "$299.99"
        }
        ["TotalNew"]=>
        string(1) "1"
        ["TotalUsed"]=>
        string(1) "0"
        ["TotalCollectible"]=>
        string(1) "0"
        ["TotalRefurbished"]=>
        string(1) "0"
      }
      ["Offers"]=>
      object(stdClass)#18 (4) {
        ["TotalOffers"]=>
        int(1)
        ["TotalOfferPages"]=>
        int(1)
        ["MoreOffersUrl"]=>
        string(190) "http://www.amazon.com/gp/offer-listing/B00IXO8I0O%3FSubscriptionId%3DAKIAJ4C2HRQEA5R5WQSA%26tag%3Dserineethi-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB00IXO8I0O"
        ["Offer"]=>
        object(stdClass)#19 (3) {
          ["Merchant"]=>
          object(stdClass)#20 (1) {
            ["Name"]=>
            string(19) "UnbeatableSale, Inc"
          }
          ["OfferAttributes"]=>
          object(stdClass)#21 (1) {
            ["Condition"]=>
            string(3) "New"
          }
          ["OfferListing"]=>
          object(stdClass)#22 (8) {
            ["OfferListingId"]=>
            string(136) "OFsamQIWSTf8O3%2F5DWBmJ633GwRX2m%2FNqcretgjz%2BoyFZJJ6QPbz8tL1YKIBYZZnOAl3AW75luhO6gbvyGV9Rr3fNI9xxUktwSzxDqtZa02RA64n3gRKtV%2BnOlgfINfn"
            ["Price"]=>
            object(stdClass)#23 (3) {
              ["Amount"]=>
              int(38999)
              ["CurrencyCode"]=>
              string(3) "USD"
              ["FormattedPrice"]=>
              string(7) "$389.99"
            }
            ["SalePrice"]=>
            object(stdClass)#24 (3) {
              ["Amount"]=>
              int(29999)
              ["CurrencyCode"]=>
              string(3) "USD"
              ["FormattedPrice"]=>
              string(7) "$299.99"
            }
            ["AmountSaved"]=>
            object(stdClass)#25 (3) {
              ["Amount"]=>
              int(9000)
              ["CurrencyCode"]=>
              string(3) "USD"
              ["FormattedPrice"]=>
              string(6) "$90.00"
            }
            ["PercentageSaved"]=>
            int(23)
            ["Availability"]=>
            string(34) "Usually ships in 4-5 business days"
            ["AvailabilityAttributes"]=>
            object(stdClass)#26 (3) {
              ["AvailabilityType"]=>
              string(3) "now"
              ["MinimumHours"]=>
              int(96)
              ["MaximumHours"]=>
              int(120)
            }
            ["IsEligibleForSuperSaverShipping"]=>
            bool(false)
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:3)

您可以像普通实例变量一样访问StdClass属性:$obj->param。例如,在这种情况下,我们可以访问' FormattedPrice'像这样:

echo $response->Items->Item->OfferSummary->LowestNewPrice->FormattedPrice; // $299.99