如何通过appcelerator中的JSON解析从对象数组中检索值

时间:2021-01-27 13:20:08

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!!!

我想得到名字“乔”这是我的想法:在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

2 个解决方案

#1


3  

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”是一个包含一个项目的数组。该项目是具有属性“B”(以及其他)的对象。所以你访问:

response[0].B.name

#2


3  

alert("hi " + response[0].B.name);

Your response is an array with an object as its first element

您的响应是一个数组,其中一个对象作为其第一个元素

#1


3  

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”是一个包含一个项目的数组。该项目是具有属性“B”(以及其他)的对象。所以你访问:

response[0].B.name

#2


3  

alert("hi " + response[0].B.name);

Your response is an array with an object as its first element

您的响应是一个数组,其中一个对象作为其第一个元素