$(document).ready(function(){
var url = 'http://api.petfinder.com/shelter.getPets?key=99392cbf55ee6b2f9b97ed375eca907d&id=WI22&status=A&output=full&format=json';
$.ajax({
type : 'GET',
data : {},
url : url+'&callback=?' ,
dataType: 'json',
\t \t
\t \t crossDomain: true,
success : function(data) {
\t var petfinder = data.petfinder;
\t \t \t
\t \t \t console.log(petfinder.pets.pet[0].media.photos.photo['1','$t','x']);
\t \t \t
\t \t }
\t })
})
<petfinder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://api.petfinder.com/schemas/0.9/petfinder.xsd">
<header>
<version>0.1</version>
<timestamp>2017-05-30T19:45:18Z</timestamp>
<status>
<code>100</code>
<message/>
</status>
</header>
<lastOffset>25</lastOffset>
<pets>
<pet>
<id>35865497</id>
<shelterId>WI22</shelterId>
<shelterPetId>0349</shelterPetId>
<name>Abby</name>
<animal>Cat</animal>
<breeds>
<breed>Domestic Short Hair-black</breed>
</breeds>
<mix>no</mix>
<age>Senior</age>
<sex>F</sex>
<size>L</size>
<options>
<option>hasShots</option>
<option>noDogs</option>
<option>altered</option>
<option>noCats</option>
<option>housetrained</option>
</options>
<description>
<![CDATA[
My name is Abby! I'm known as the pretty office cat at the Humane Society, but I'm still up for adoption! I'm almost 11 years old but I'm still pretty active for my age! I was surrendered due to my owner having allergies, so it was no fault of my own! That was back in June, 2016! I don't like being picked up due to a medical complication that I have, but other than that I LOVE attention. I LOVE being petted! (Even if I shed sometimes!) I love to cuddle and, quietly, sit with the staff! I'm pretty shy at first, but it won't take me long to warm up to you! I would really enjoy a home that I can spend my golden years at! If you have any questions about me, don't hesitate to call the Humane Society! For more information on this cat or any other animals available at the Humane Society of Barron County please contact us! 715-537-9063, [email protected] or www.hsbcshelter.com
]]>
</description>
<lastUpdate>2017-05-27T15:44:18Z</lastUpdate>
<status>A</status>
<media>
<photos>
<photo id="1" size="pnt">
http://photos.petfinder.com/photos/pets/35865497/1/?bust=1488416755&width=60&-pnt.jpg
</photo>
<photo id="1" size="fpm">
http://photos.petfinder.com/photos/pets/35865497/1/?bust=1488416755&width=95&-fpm.jpg
</photo>
<photo id="1" size="x">
http://photos.petfinder.com/photos/pets/35865497/1/?bust=1488416755&width=500&-x.jpg
</photo>
<photo id="1" size="pn">
http://photos.petfinder.com/photos/pets/35865497/1/?bust=1488416755&width=300&-pn.jpg
</photo>
<photo id="1" size="t">
http://photos.petfinder.com/photos/pets/35865497/1/?bust=1488416755&width=50&-t.jpg
</photo>
<photo id="2" size="pnt">
http://photos.petfinder.com/photos/pets/35865497/2/?bust=1488416755&width=60&-pnt.jpg
</photo>
<photo id="2" size="fpm">
http://photos.petfinder.com/photos/pets/35865497/2/?bust=1488416755&width=95&-fpm.jpg
</photo>
<photo id="2" size="x">
http://photos.petfinder.com/photos/pets/35865497/2/?bust=1488416755&width=500&-x.jpg
</photo>
<photo id="2" size="pn">
http://photos.petfinder.com/photos/pets/35865497/2/?bust=1488416755&width=300&-pn.jpg
</photo>
<photo id="2" size="t">
http://photos.petfinder.com/photos/pets/35865497/2/?bust=1488416755&width=50&-t.jpg
</photo>
</photos>
</media>
<contact>
<address1>1571 Guy Avenue</address1>
<address2/>
<city>Barron</city>
<state>WI</state>
<zip>54812</zip>
<phone>715-537-9063</phone>
<fax>715-637-0108</fax>
<email>[email protected]</email>
</contact>
</pet>
{"@size":"x","$t":"http://photos.petfinder.com/photos/pets/35865497/1/? bust=1488416755&width=500&-x.jpg","@id":"1"}
我試圖使用JavaScript訪問的照片在這個XML(返回由AJAX作爲JSON)。我不知道如何檢索圖像,因爲它們的格式如上所述。任何幫助,將不勝感激?或者只是一些方向。
編輯:console.log(petfinder.pets.pet[0].media.photos.photo['1','$t','x']);
剛剛返回undefined
呀那些方括號逗號分隔的值不是訪問屬性o的有效方式如果你是一個JavaScript對象,你希望那行代碼做什麼? – James
我只想在該JSON字符串中檢索第一個寵物的第一張圖片(如圖所示)。但我不知道如何格式化這一行。 –
'console.log(petfinder.pets.pet [0] .media.photos.photo [0] ['$ t']);'應該輸出url。那些奇怪的關鍵名稱似乎是因爲它們更習慣於提供XML文檔,並且@對應於屬性,而$ t對應於元素的文本。 – James