0

以下のコードは機能するはずなので、これはばかげているように聞こえます。

document.getElementById('petimg').src = petJSON[0].picture[0].large;

ただし、サイトがクラッシュし続けます

ここに完全なコードがあります

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://service.ipetfindr.com/iOS/?uri=fetchpet/13373A" + n[1],false);
xmlhttp.send();

var petJSON = JSON.parse(xmlhttp.responseText);

alert(petJSON[0].picture.large);

document.getElementById('petimg').src = petJSON[0].picture.[0].large;
document.getElementById("petname").innerHTML = petJSON[0].petname;
document.getElementById("breed").innerHTML = petJSON[0].breed;
document.getElementById("petid").innerHTML = petJSON[0].ipetfindrtagid;

私が問題を抱えているJSONの部分は次のとおりです

"picture":[{"large":"http:\/\/www.ipetfindr.com\/petuploads\/7b2b07363b271703782d0b7d5362f8f4.JPG","small":"http:\/\/www.ipetfindr.com\/petuploads\/7b2b07363b271703782d0b7d5362f8f4-x-h80.JPG"}]

EDIT FIX このエラーを修正する方法は簡単でした

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://service.ipetfindr.com/iOS/?uri=fetchpet/13373A" + n[1],false);
xmlhttp.send();

var petJSON = JSON.parse(xmlhttp.responseText);
var petpicture = petJSON[0].picture[0];
alert(petpicture.large); //THIS allows me or anyone to call the large image in the sub array

document.getElementById('petimg').src = petJSON[0].picture.[0].large;
document.getElementById("petname").innerHTML = petJSON[0].petname;
document.getElementById("breed").innerHTML = petJSON[0].breed;
document.getElementById("petid").innerHTML = petJSON[0].ipetfindrtagid;
4

1 に答える 1

0

petJSON[0].picture.[0].large構文エラーです。の前からドットを削除します。プロパティにアクセス[するには、ドットまたはブラケット表記のみを使用できます。

于 2013-03-02T16:20:12.240 に答える