无法访问Javascript对象属性?

时间:2014-07-26 17:19:13

标签: javascript json

我有一个从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出现了#34;未定义"。这里发生了什么?我也尝试过使用数组访问器。

2 个答案:

答案 0 :(得分:0)

尝试

document.getElementById('yelp-rating').setAttribute('src', yelpObj.rating_img_url);

答案 1 :(得分:-1)

对于疏忽感到抱歉 - 该日志表明它实际上不是一个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…}

实际上是一个对象。

当然,访问者也可以工作。对不起,请稍等一下。谢谢你看看。

相关问题