2013-10-29 95 views
0

我有一個看起來像這樣

[{"id":"1","name":"Muslin","image1":"muslin.jpg"},{"id":"72","name":"Gagra choli","image1":"gagra choli.jpg"},{"id":"73","name":"Lahenga saree","image1":"lahenga saree.jpg"},{"id":"74","name":"Ao dai","image1":"Ao dai.jpg"},{"id":"75","name":"Brocade","image1":"brocade.jpg"},{"id":"77","name":"Button","image1":"button.jpg"},{"id":"78","name":"Bathrobe","image1":"Bathrobe.jpg"},{"id":"79","name":"Bathtowel","image1":"Bathtowel.jpg"},{"id":"80","name":"Cassock","image1":"cassock.jpg"},{"id":"81","name":"Shirt","image1":"shirt.jpg"},{"id":"82","name":"Dolce and Gabanna","image1":"dolce_and_gabanna.jpg"},{"id":"83","name":"asasa","image1":"1009578_1392050054339713_1292927121_o.jpg"},{"id":"84","name":"knbdfjhbjhbvjh","image1":"images_(1)10.jpg"}] 

現在我使用了一些這樣的代碼

var obj=$.parseJSON(JSON.stringify(res)); 
       $('#response').html(obj.id); 

但控制檯說,obj.id未定義一個JSON數據

+0

什麼是「res」變量?我懷疑你錯誤地做了這件事。 –

+0

你可以訪問id爲obj [0] .id不是obj.id – abc123

+0

該數組中有很多ID,你想在HTML中放入哪一個ID? – Barmar

回答

1

var obj是對象的數組。所以你需要使用索引。

$('#response').html(obj[0].id); 
+0

看到我不能接受在9分鐘內的答案,所以請稍候.... – user2889070

0
var obj=$.parseJSON(JSON.stringify(res)); 

將對象字符串化爲字符串,然後將其解析回對象。爲什麼不直接使用res

res是一個數組,如果你想獲得id,那麼你必須使用索引。

例如:

$('#response').html(res[0].id);