2013-01-05 43 views
1

我想遍歷一個JSON數組以達到size:large的URL。 #text屬性讓我感到困惑,因爲我無法將它包含在我的點符號中。我能到size=large與此:Json解析#text對象

json.track.album.image[2].size 

json.track.album.image[2].#text將無法​​正常工作。

這裏是JSON數據的一個片段:

{"track":{"id":"1019817","name":"Believe","mbid":"13194c93-89c6-4ab4-aaf2-  15db5d73b74e","url":"http:\/\/www.last.fm\/music\/Cher\/_\/Believe","duration":"222000","streamable":{"#text":"1","fulltrack":"0"},"listeners":"287271","playcount":"1206650","artist":{"name":"Cher","mbid":"bfcc6d75-a6a5-4bc6-8282-47aec8531818","url":"http:\/\/www.last.fm\/music\/Cher"},"album":{"artist":"Cher","title":"The Very Best of Cher","mbid":"5df00011-6e57-4837-99ae-61f398481db5","url":"http:\/\/www.last.fm\/music\/Cher\/The+Very+Best+of+Cher","image":[{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64s\/71997588.png","size":"small"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/71997588.png","size":"medium"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/174s\/71997588.png","size":"large"},{"#text":"http:\/\/userserve-ak.last.fm\/serve\/300x300\/71997588.png","size":"extralarge"}], 

回答

1

因爲領先#,你必須使用[]語法來代替:

json.track.album.image[2]['#text'] 

這允許您指定鍵一個字符串,所以特殊字符是允許的。

+0

謝謝詹姆斯。就像所有JS一樣,答案總是很簡單,但是到達那裏...... – hillcreative