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 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行