I have the following JSON object in javascript returned by a PHP web service :
data={
"result": "pass",
"error_type": "",
"feedback_ids": {
"feedback0": "1",
"feedback1": "8"
},
"redirect_uri": ""
}
alert(data.result) works like a peach. How do I access "feedback_ids" and alert feedback0 and feedback1?
-
may be u havent parsed the JSON. Parse the JSON and then try it . var parsedObj = JSON.parse(data) ; alert(parsedObj.result);user844866– user8448662013年03月05日 19:49:33 +00:00Commented Mar 5, 2013 at 19:49
4 Answers 4
To access those variables you need to do:
data.feedback_ids.feedback0
and
data.feedback_ids.feedback1
If you have flexibility on server, make your life easier by changing that to JSON Array so you could loop them by their index. Note below I just changed the feedback_ids to the array structure
data={
"result": "pass",
"error_type": "",
"feedback_ids": [
"1",
"8"
],
"redirect_uri": ""
}
7 Comments
To get at the feedback_ids:
ids = data.feedback_ids;
And to get inside that:
one = data.feedback_ids.feedback0;
eight = data.feedback_ids.feedback1;
You could also use array notation:
one = data['feedback_ids']['feedback0'];
1 Comment
data.feedback_ids.feedback0 and data.feedback_ids.feedback1
Comments
for (var k in data.feedback_ids) {
console.log(k, data.feedback_ids[k]);
}
This code will output:
feedback0, 1
feedback1, 8