简体   繁体   English

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

[英]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.

相关问题 JSON-对象数组中的对象-如何使用JS解析循环? - JSON - object in array in object - how to parse through loop with JS? 如何在appcelerator合金中将listview值从一个窗口解析到另一个窗口 - how to parse the listview value from one window to another in appcelerator alloy 从JSON响应Appcelerator Cloud解析图像? - Parse an image from json response Appcelerator Cloud? 如何将json从特定的数组字符串解析为数组对象? - How to parse json from the specific array string to array object? JSON.Parse() 无法正确检索 json 数组的值 - JSON.Parse() not able to retrieve the value of json array properly 如何从 JSON 对象的值数组中检索键? - How to retrieve the key from an array of values of a JSON object? 如何从JSON对象数组中检索不同类型的数据并进行处理? - How to retrieve different type of data from JSON object array and play with it? 如何从NodeJS中的Json对象列表中检索特定值 - How to retrieve specific value from list of Json Object in NodeJS 如何从存储在sessionStorage中的JSON值检索特定对象? - How to retrieve a specific object from a JSON value stored in sessionStorage? Vanilla JS:如何检索 json 文件,将结果解析为数组,并访问每个 object - Vanilla JS: How to retrieve json file, parse the results into an array, and access each object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM