简体   繁体   中英

Display json content in php

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&_ip g=1&_mPrRngCbx=1&_os=ST%7CD&_pgn=1&_sid=181133160&_ssn=sewlovelypatterns&_udlo=11"]}]}

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: eg 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM