Can';无法访问Javascript对象属性?
我从JSON结果中解析了一个Javascript对象。出于某种原因,我对它的任何属性都没有定义 代码: 我的日志:Can';无法访问Javascript对象属性?,javascript,json,Javascript,Json,我从JSON结果中解析了一个Javascript对象。出于某种原因,我对它的任何属性都没有定义 代码: 我的日志: { "is_claimed": false, "rating": 3.5, "mobile_url": "http://m.yelp.com/biz/o-griffs-grill-and-brewhouse-quincy", "rating_img_url": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/5ef
{
"is_claimed": false,
"rating": 3.5,
"mobile_url": "http://m.yelp.com/biz/o-griffs-grill-and-brewhouse-quincy",
"rating_img_url": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/5ef3eb3cb162/ico/stars/v1/stars_3_half.png",
"review_count": 12,
"name": "O'Griff's Grill and Brewhouse",
"snippet_image_url": "http://s3-media1.fl.yelpcdn.com/photo/eD7wryocRyM496sbYWV5XQ/ms.jpg",
"rating_img_url_small": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/2e909d5d3536/ico/stars/v1/stars_small_3_half.png",
"url": "http://www.yelp.com/biz/o-griffs-grill-and-brewhouse-quincy",
"reviews": [
{
"rating": 5,
"excerpt": "Great food, knowledgeable and patient staff members. They even take the time to help you out of the door if you happen to be in a wheelchair.",
"time_created": 1397543825,
"rating_image_url": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
"rating_image_small_url": "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
"user": {
"image_url": "http://s3-media1.fl.yelpcdn.com/photo/eD7wryocRyM496sbYWV5XQ/ms.jpg", "id": "4wyHlQ08i793eeht10NJvg",
"name": "Tyler M."
},
"rating_image_large_url": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
"id": "my6lHhYBSrbo4ty_s4Zvew"
}
],
"phone": "2172242002",
"snippet_text": "Great food, knowledgeable and patient staff members. They even take the time to help you out of the door if you happen to be in a wheelchair.",
"image_url": "http://s3-media4.fl.yelpcdn.com/bphoto/srJnO4BdberFKA_bNf7hCg/ms.jpg",
"categories": [
[
"American (Traditional)",
"tradamerican"
]
],
"display_phone": "+1-217-224-2002",
"rating_img_url_large": "http://s3-media3.fl.yelpcdn.com/assets/2/www/img/bd9b7a815d1b/ico/stars/v1/stars_large_3_half.png",
"id": "o-griffs-grill-and-brewhouse-quincy",
"is_closed": false,
"location": {
"city": "Quincy",
"display_address": [
"415 Hampshire St",
"Quincy, IL 62301"
],
"postal_code": "62301",
"country_code": "US",
"address": [
"415 Hampshire St"
],
"state_code": "IL"
}
}
但src显示为“未定义”。这是怎么回事?我也尝试过使用数组访问器。试试
document.getElementById('yelp-rating').setAttribute('src', yelpObj.rating_img_url);
抱歉疏忽了-日志表明它实际上不是Javascript对象-不确定它是什么。Yelp自动返回一个JSON对象,所以我不需要在PHP端对其进行编码。日志现在是这样的-
Object {is_claimed: false, rating: 3.5, mobile_url: "http://m.yelp.com/biz/o-griffs-grill-and-brewhouse-quincy", rating_img_url: "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/5ef3eb3cb162/ico/stars/v1/stars_3_half.png", review_count: 12…}
这实际上是一个物体
当然,访问器也可以工作。很抱歉给您带来麻烦-感谢您的关注。什么是元素
yelp评级
?请尝试yelpObj['rating\u img\u url']
Object {is_claimed: false, rating: 3.5, mobile_url: "http://m.yelp.com/biz/o-griffs-grill-and-brewhouse-quincy", rating_img_url: "http://s3-media1.fl.yelpcdn.com/assets/2/www/img/5ef3eb3cb162/ico/stars/v1/stars_3_half.png", review_count: 12…}