doukuanghuan7582 2015-03-03 22:11
浏览 32
已采纳

在php中显示json内容

I have this json file that I get from ebay api and I'm trying to echo it. How can I reach specific values such as title, galleryURL, viewItemURL etc.

This is how I tried and getting error

<?php
$json = file_get_contents('ebay api url');
$obj = json_decode($json);
echo $obj-> item[0]->title;

?>

I placed item[0] because usualy I recieve bunch of items inside json And this is json file

{"findItemsIneBayStoresResponse":[{"ack":["Success"],"version":["1.13.0"],"timestamp":["2015-03-03T22:06:11.764Z"],"searchResult":[{"@count":"1","item":[{"itemId":["121579650061"],"title":["McCall's 9119 18\" Doll PatternCarry Case and Accessories Carry Case Doll Trunk "],"globalId":["EBAY-US"],"primaryCategory":[{"categoryId":["38057"],"categoryName":["Dolls, Toys, Animals"]}],"galleryURL":["http://thumbs2.ebaystatic.com/m/mBpbx-hUvsUvTKYMVasM8xA/140.jpg"],"viewItemURL":["http://www.ebay.com/itm/McCalls-9119-18-Doll-PatternCarry-Case-and-Accessories-Carry-Case-Doll-Trunk-/121579650061?pt=LH_DefaultDomain_0"],"paymentMethod":["PayPal"],"autoPay":["false"],"postalCode":["85260"],"location":["Scottsdale,AZ,USA"],"country":["US"],"storeInfo":[{"storeName":["Sew Lovely Patterns"],"storeURL":["http://stores.ebay.com/Sew-Lovely-Patterns"]}],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","value":"0.0"}],"shippingType":["FlatDomesticCalculatedInternational"],"shipToLocations":["Worldwide"],"expeditedShipping":["false"],"oneDayShippingAvailable":["false"],"handlingTime":["1"]}],"sellingStatus":[{"currentPrice":[{"@currencyId":"USD","value":"21.99"}],"convertedCurrentPrice":[{"@currencyId":"USD","value":"21.99"}],"sellingState":["Active"],"timeLeft":["P22DT22H41M24S"]}],"listingInfo":[{"bestOfferEnabled":["false"],"buyItNowAvailable":["false"],"startTime":["2015-02-24T20:42:35.000Z"],"endTime":["2015-03-26T20:47:35.000Z"],"listingType":["StoreInventory"],"gift":["false"]}],"returnsAccepted":["true"],"galleryPlusPictureURL":["http://galleryplus.ebayimg.com/ws/web/121579650061_1_0_1.jpg"],"condition":[{"conditionId":["1000"],"conditionDisplayName":["New"]}],"isMultiVariationListing":["false"],"topRatedListing":["false"]}]}],"paginationOutput":[{"pageNumber":["1"],"entriesPerPage":["1"],"totalPages":["117"],"totalEntries":["117"]}],"itemSearchURL":["http://stores.ebay.com/Sew-Lovely-Patterns/_i.html?_saslop=1&_fss=1&LH_SpecificSeller=1&_ddo=1&_exmaitems=1&_ipg=1&_mPrRngCbx=1&_os=ST%7CD&_pgn=1&_sid=181133160&_ssn=sewlovelypatterns&_udlo=11"]}]}

  • 写回答

1条回答 默认 最新

  • dongrao9454 2015-03-03 22:26
    关注

    All you need is a little analysis of the JSON. Just write this into a file (say response.json) and execute the following of the JSON.

    cat response.json | python -mjson.tool
    

    Now you can observe the full hierarchy in the JSON. Use the following to convert it into an PHP Associative Array.

    $obj = json_decode($json, true);    // note the 'true'
    

    Once done with that, here is how you access via indexes easily: e.g. to get the title, you need the following access

    $obj["findItemsIneBayStoresResponse"][0]["searchResult"][0]["title"]
    

    This is what I figured out by pretty printing the JSON. I would suggest you refer to the eBay API on how it structures the hierarchy of the response JSON. If you don't know how to read associative arrays, read some examples at [PHP Arrays Documentation].(http://php.net/manual/en/language.types.array.php).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)