我有此JSON对象:JSON解析与jQuery
({a1:-1,a2:null, messages:[{b1:message1, b2:message2, b3:message3, ... }]})
我如何通过使用jQuery的。每个消息对环路(没有硬编码的B1,B2,B3,MESSAGE1,消息2,消息3) 。
我有此JSON对象:JSON解析与jQuery
({a1:-1,a2:null, messages:[{b1:message1, b2:message2, b3:message3, ... }]})
我如何通过使用jQuery的。每个消息对环路(没有硬编码的B1,B2,B3,MESSAGE1,消息2,消息3) 。
假设data
是你的对象,你可以使用$.each
这个。
var messages = data.messages;
$.each(messages, function(i, msg){
$.each(msg, function(key, message){
console.log(key+': '+message);
});
});
首先阅读reference for jQuery.each()
。这里有个参考的例子:
$.each({ name: "John", lang: "JS" }, function(k, v) {
alert("Key: " + k + ", Value: " + v);
});
你期望有人阅读文档的某些东西?现在认真,通过很好。 :) – Eonasdan
感谢您的回复。有时它只是一个例子,让它点击。我现在意识到我需要每个嵌套。很酷。 –
什么?问题是什么? – Zirak
注意,这是无效的JSON。 –
值得一提的是,没有像“JSON对象”这样的东西! JSON根据定义是一个字符串。只要它不再是一个字符串,它只是一个JavaScript对象! –