getting at these ajax values
I have an ajax request that returns the following.
{
"field1":{"label1":"message1"},
"field2":{"lable1":"message1"}
}
How can I reaad the values of "field1" and message1? The problem is that I don't know the names of these labels, so right now they're label1, label2, but开发者_JS百科 it varies.
I'm trying this, but it gives me an object.
for (oneline in response) {
alert(response[oneline]);
}
I assume you already parsed the JSON, as you say you get an object. You can access it then like a normal JavaScript object:
for (var data in response) {
for(var message in response[data]) {
alert(response[date][message]);
}
}
Edit: Updated as the fields of the objects are unknown too.
oneline
will have the values field1
and field2
.
for (var oneline in response) {
alert(response[oneline].label1);
}
alert(response.field1.label1)
for (oneline in response) {
alert(JSON.stringify(oneline));
}
JSON.parse(), the opposite of what you want, is used by jQuery() http://api.jquery.com/jQuery.parseJSON/
JSON.stringify({whatever:values}); will spit out a string of any valid JSON.
精彩评论