[英]Can't access Javascript object properties?
我有一個從JSON結果解析的Javascript對象。 由於某種原因,我對其任何屬性都不確定。
碼:
var yelpObj = JSON.parse(result);
console.log(yelpObj);
document.getElementById('yelp-rating').src=yelpObj.rating_img_url;
我的日志:
{
"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…}
這實際上是一個對象。
當然,訪問器也可以工作。 很抱歉給您帶來麻煩-感謝您的關注。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.