[英]how to retrieve a value from an object array through JSON parse in appcelerator
assume a fake json response, i have this json string... 假设一个假的json响应,我有这个json字符串...
[{"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: in JAVASCRIPT for an iphone app!!! 我想得到的名字是“ joe”,这就是我的想法:在JAVASCRIPT中,一个iPhone应用程序!!!
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 rupGo
alert("hi " + response[0].B.name);
您的响应是一个以对象为第一个元素的数组
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). 该项目是一个具有属性“ B”(以及其他)的对象。 So you'd access: 因此,您可以访问:
response[0].B.name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.