简体   繁体   English

Javascript JSON.parse SUB JSON

[英]Javascript JSON.parse SUB JSON

Ok I know this sounds stupid, because the code below should be working 好的,我知道这听起来很愚蠢,因为下面的代码应该可以正常工作

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

however it keeps crashing the site 但是它不断使网站崩溃

here is the full code 这是完整的代码

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;

The part of the JSON I am having an issue with is the following 我遇到问题的JSON部分如下

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

EDIT FIX the way to fix this error was simple 编辑修复解决此错误的方法很简单

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;

petJSON[0].picture.[0].large is a syntax error. petJSON[0].picture.[0].large是语法错误。 Remove the dot from before the [ , you can only use either dot or bracket notation to access properties . 删除[之前的点,您只能使用点或括号符号来访问属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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