简体   繁体   中英

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

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). So you'd access:

response[0].B.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM