2014-07-26 2 views
0

JSON 결과에서 파싱 된 Javascript 객체가 있습니다. 웬일인지, 나는 그 속성에 대해 정의되지 않고있다.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는 "정의되지 않은"변합니다. 여기서 무슨 일이 일어나고있는거야? 또한 배열 접근자를 사용하여 시도했다.

+0

'yelp-rating' 요소는 무엇입니까? – CodingGorilla

+0

yelpObj 시도 [ 'rating_img_url'] – V31

답변

0

은 감독에 대한

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

죄송 시도 - 그게 뭔지 확실하지 - 해당 로그는 사실 하지 자바 스크립트 객체라고 지적했다. 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…} 

- 로그는 지금이다.

그리고 물론 접근자가 작동합니다. 문제를 일으켜서 죄송합니다. 한번 들여 보내 주셔서 감사합니다.

관련 문제