how to retrieve a value from an object array through JSON parse in appcelerator
assume a fake json response, i have this json string...
[{"A":"1","B":{"name":"joe","lastname":"jones"},"COLORS:{"red":"rojo","blue":"azul"},"active":"yes"}]
i want to get the name "joe" this is what i thought:开发者_如何学Go in JAVASCRIPT for an iphone app!!!
var json = this.responseText;
var response = JSON.parse(json);
alert("hi " + response.B.name);
//the output should be " hi joe"!!
but there is no response.... the alert goes blank... any help would be apreciated
rupGo
Your posted example has some syntax issues. I assume that was simple an error in your example preparation, and not actually in your code. Corrected and formatted, it looks like:
[
{
"A": "1",
"B": {
"name": "joe",
"lastname": "jones"
},
"COLORS": {
"red": "rojo",
"blue": "azul"
},
"active": "yes"
}
]
In your response example, 'response' is an array with one item. That item is an object which has property 'B' (among others). So you'd access:
response[0].B.name
alert("hi " + response[0].B.name);
Your response is an array with an object as its first element
精彩评论