0

我正在尝试解码从 ebay 查找 API 收到的 JSON 响应。我怎样才能让它回显ack消息?

这是json响应:

{
   "findItemsByCategoryResponse":[
      {
         "ack":[
            "Success"
         ],
         "version":[
            "1.12.0"
         ],
         "timestamp":[
            "2013-08-23T10:12:22.085Z"
         ],
         "searchResult":[
            {
               "@count":"1",
               "item":[
                  {
                     "itemId":[
                        "121158900630"
                     ],
                     "title":[
                        "SHAMROCK SHORES, TX - MONTHLY PAYMENTS - 0 INTEREST"
                     ],
                     "globalId":[
                        "EBAY-US"
                     ],
                     "subtitle":[
                        "blocks from Lake Brownwood"
                     ],
                     "primaryCategory":[
                        {
                           "categoryId":[
                              "15841"
                           ],
                           "categoryName":[
                              "Land"
                           ]
                        }
                     ],
                     "galleryURL":[
                        "http:\/\/thumbs3.ebaystatic.com\/m\/mP-wKIofn8JI9YsMduq6eUw\/140.jpg"
                     ],
                     "viewItemURL":[
                        "http:\/\/www.ebay.com\/itm\/SHAMROCK-SHORES-TX-MONTHLY-PAYMENTS-0-INTEREST-\/121158900630?pt=Land"
                     ],
                     "autoPay":[
                        "false"
                     ],
                     "postalCode":[
                        "76801"
                     ],
                     "location":[
                        "Brownwood,TX,USA"
                     ],
                     "country":[
                        "US"
                     ],
                     "shippingInfo":[
                        {
                           "shippingType":[
                              "FreePickup"
                           ],
                           "shipToLocations":[
                              "None"
                           ],
                           "expeditedShipping":[
                              "false"
                           ],
                           "oneDayShippingAvailable":[
                              "false"
                           ]
                        }
                     ],
                     "sellingStatus":[
                        {
                           "currentPrice":[
                              {
                                 "@currencyId":"USD",
                                 "__value__":"26.0"
                              }
                           ],
                           "convertedCurrentPrice":[
                              {
                                 "@currencyId":"USD",
                                 "__value__":"26.0"
                              }
                           ],
                           "bidCount":[
                              "4"
                           ],
                           "sellingState":[
                              "Active"
                           ],
                           "timeLeft":[
                              "P0DT1H12M13S"
                           ]
                        }
                     ],
                     "listingInfo":[
                        {
                           "bestOfferEnabled":[
                              "false"
                           ],
                           "buyItNowAvailable":[
                              "false"
                           ],
                           "startTime":[
                              "2013-08-13T11:24:35.000Z"
                           ],
                           "endTime":[
                              "2013-08-23T11:24:35.000Z"
                           ],
                           "listingType":[
                              "Auction"
                           ],
                           "gift":[
                              "false"
                           ]
                        }
                     ],
                     "returnsAccepted":[
                        "false"
                     ],
                     "isMultiVariationListing":[
                        "false"
                     ],
                     "topRatedListing":[
                        "false"
                     ]
                  }
               ]
            }
         ],
         "paginationOutput":[
            {
               "pageNumber":[
                  "1"
               ],
               "entriesPerPage":[
                  "1"
               ],
               "totalPages":[
                  "631"
               ],
               "totalEntries":[
                  "631"
               ]
            }
         ],
         "itemSearchURL":[
            "http:\/\/www.ebay.com\/sch\/15841\/i.html?_ddo=1&_ipg=1&_pgn=1"
         ]
      }
   ]
}

以下是我的php代码:

$url = "http://svcs.ebay.com/services/search/FindingService/v1?" . 
        "OPERATION-NAME=findItemsByCategory&" . 
        "SERVICE-VERSION=1.0.0&" .
        "SECURITY-APPNAME=YourKeyHere&" . 
        "RESPONSE-DATA-FORMAT=JSON&" . 
        "REST-PAYLOAD&" . 
        "categoryId=15841&" . 
        "paginationInput.entriesPerPage=1";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch); 
curl_close($ch);     

$json = json_decode($output, true);

$status = $json['findItemsByCategory']['ack'];
echo $status;
4

2 回答 2

2

findItemsByCategoryResponse是一个数组,它的第一个元素有ack

$status = $json['findItemsByCategoryResponse'][0]['ack'];
// -------------------------------------------^^^

奇怪的是,ack它也是一个数组,因此要获取文本"Success",您也必须取消引用它:

$status = $json['findItemsByCategoryResponse'][0]['ack'][0];
// -------------------------------------------^^^-------^^^
于 2013-08-23T10:20:47.807 回答
0

假设 $json 包含收到的 json 响应,您可以使用以下内容:

$obj = json_decode($json);
$status = $obj->findItemsByCategoryResponse[0]->ack;
于 2013-08-23T10:55:02.140 回答