从数组集合中读取单个数据
我使用 var dp:ArrayCollection = new ArrayCollection(container.GetVotesResult); 从 GetVotesResult
方法获取 JSON 数据。我得到的值如下。
{"GetVotesResult":
[{"Aid":0,"Arank":0,"Atext":null,"ClientId":16,"Votes":0,"qid":10,"qtext":"Who will win 2011 football world cup?"},
{"Aid":4,"Arank":1,"Atext":"yes","ClientId":null,"Votes":0,"qid":null,"qtext":null},
{"Aid":5,"Arank":2,"Atext":"no","ClientId":null,"Votes":0,"qid":null,"qtext":null},
{"Aid":6,"Arank":3,"Atext":"i don't know","ClientId":null,"Votes":0,"qid":null,"qtext":null}]}
我能够在循环 dp arraycollection 列表后检索第一个数组数据。
if(i==0)
{
trace("Show me:",obj.qtext);
}
O/P: Show me: Who will win 2011 football world cup?
如何单独且动态地检索第二、第三、第四等(如果有)数组数据。比如说,我想从所有数组中取出“Atext”。请帮忙。我用的是flashbuilder4.5..
I used var dp:ArrayCollection = new ArrayCollection(container.GetVotesResult);
to get the JSON data from GetVotesResult
method. I get the values as below.
{"GetVotesResult":
[{"Aid":0,"Arank":0,"Atext":null,"ClientId":16,"Votes":0,"qid":10,"qtext":"Who will win 2011 football world cup?"},
{"Aid":4,"Arank":1,"Atext":"yes","ClientId":null,"Votes":0,"qid":null,"qtext":null},
{"Aid":5,"Arank":2,"Atext":"no","ClientId":null,"Votes":0,"qid":null,"qtext":null},
{"Aid":6,"Arank":3,"Atext":"i don't know","ClientId":null,"Votes":0,"qid":null,"qtext":null}]}
I am able to retrieve the 1st array data after looping the dp arraycollection list.
if(i==0)
{
trace("Show me:",obj.qtext);
}
O/P: Show me: Who will win 2011 football world cup?
How do I retrieve the 2nd, 3rd, 4th and so on (if it has) array datas individually and dynamically. Say, I want to take 'Atext' from all the array. Please help. I use flashbuilder4.5..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用filter() 和map() 创建所需数据的新数组。
假设您已经将 JSON 数据放入 arrayCollection(或数组)中,因此对于本示例,我只是创建数组:
现在您可以使用 Array.filter 创建一个新数组,该数组仅包含具有有效值的元素所需字段的值:
要测试上述内容:
或者,您可以使用 Array.map 创建一个仅包含某个属性的值的数组:
您可以使用以下方法测试上面的映射函数:
此页面很好地描述了映射、过滤器,以及数组的其余部分:http://www.onebyonedesign.com/tutorials/array_methods/
You can use filter() and map() to create new arrays of the data you need.
Let's assume you're already getting the JSON data into in arrayCollection (or array), so for this example I'm just creating the array:
Now you can use Array.filter to create a new array that only contains elements that have a valid value for the desired field:
To test the above:
Or, you can use Array.map to create an array of only values for a certain property:
You can test the map function above with:
This page does a great job describing map, filter, and the rest of Array: http://www.onebyonedesign.com/tutorials/array_methods/
从上面的输出来看,GetVotesResult 是一个对象数组,您可以使用
for
/foreach
循环对其进行迭代,例如:或者,您可能希望将对象复制到映射数据结构(AS3 中的
字典
)以创建查找表基于其中之一按键:From your output above, GetVotesResult is an Array of Objects which you can iterate over using a
for
/for each
loop, eg:Alternatively you may wish to copy the objects into a Map Data Structure (a
Dictionary
in AS3) to create a lookup table based on one of the Keys:这可以从该对象获取所有字符串
this can get all String from that object