2012-11-08 32 views
0

我想打印出「消息」jQuery的給我「未定義」呼應JSON

JSON:

[{"outcome":false,"message":"This is the message"}] 

我試過3樣東西,這裏是他們的成果...

console.log(msg) =>[{"outcome":false,"message":"Your account is pending beta activation"}](這是好的)

var x = $.parseJSON(msg);

console.log(x)給了我一個對象

console.log(x.message)給我undefined

什麼是訪問msg.message正確的方式,如果不是這樣的?

回答

3

你有一個數組中的對象。使用索引0

x[0].message; 

如果你在Array期待更多的對象,你可以使用$.each遍歷數組。

$.each(x, function(i, obj) { 
    console.log(obj.message); 
}); 
2

它看起來像你獲得包含數組返回JSON對象,你嘗試:

console.log(x[0].message); 
1

您的JSON是一個數組對象的

相反,你應該使用

x[0].message;