我使用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?
如何单独和动态地检索第2,第3,第4等(如果有)数组数据。说,我想从所有数组中获取'Atext'。请帮忙。我用flashbuilder4.5 ..
答案 0 :(得分:1)
您可以使用filter()和map()来创建所需数据的新数组。
假设您已经在arrayCollection(或数组)中获取了JSON数据,因此对于此示例,我只是创建数组:
private var GetVotesResult:Array = [{"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":"Who stole my socks?"},
{"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}];
现在您可以使用Array.filter创建一个新数组,该数组仅包含具有所需字段的有效值的元素:
//Get an array with elements that have the desired property:
public function getElementsWithProperty( propName:String ):Array {
return GetVotesResult.filter( elementHasProp( propName ) );
}
private function elementHasProp( propName:String ):Function {
return function( element:Object, index:int, array:Array ):Boolean {
return ( element[ propName ] != null );
}
}
测试上述内容:
var elementsWithQText:Array = getElementsWithProperty( 'qtext' );
trace( 'Values of qtext in elementsWithQText array: ' );
for each ( var element:Object in elementsWithQText ) {
trace( element.qtext );
}
//OUTPUT:
//Values of qtext in elementsWithQText array:
//Who will win 2011 football world cup?
//Who stole my socks?
或者,您可以使用Array.map为特定属性创建仅包含值的数组:
//Get an array of only a certain property:
public function makeArrayOfProperty( propName:String ):Array {
return GetVotesResult.map( valueOfProp( propName ) );
}
private function valueOfProp( propName:String ):Function {
return function( element:Object, index:int, array:Array):String {
return element[ propName ];
}
}
您可以使用以下方法测试上面的地图功能:
var valuesOfAtext:Array = makeArrayOfProperty( 'Atext' );
trace( 'Values of valuesOfAtext: ' + valuesOfAtext );
//OUTPUT: Values of valuesOfAtext: ,yes,no,i don't know
此页面可以很好地描述地图,过滤器以及数组的其余部分:http://www.onebyonedesign.com/tutorials/array_methods/
答案 1 :(得分:0)
这可以从该对象获取所有String
for(var i=0;i<data.length;i++){
for(var key in d){
if(d[key] instanceof String)
trace(d[key]);
}
}
答案 2 :(得分:0)
从上面的输出中,GetVotesResult是一个对象数组,您可以使用for
/ for each
循环进行迭代,例如:
var result : String = ""; // JSON String omitted for brevity
// Decode the JSON String into an AS3 Object graph.
var data : Object = JSON.decode(result);
// reference the GetVotesResult Array from the result Object.
var votes : Array = data["GetVotesResult"];
// Iterate over each 'Vote' object in turn and pull out the
// 'Atext' values the objects contain into a new Array.
var Atexts : Array = [];
for each (var vote : Object in votes)
{
// Check for the existance of the 'aText' property.
if (vote.hasOwnProperty("Atext")) {
Atexts.push(vote.Atext);
}
}
// Dump out all the aText values: (,yes, no, i)
trace("Atexts: " + Atexts.join(", "));
或者,您可能希望将对象复制到地图数据结构(AS3中的Dictionary
)以基于其中一个键创建lookup table:
// Create a new, empty Lookup Table.
var votesByAid : Dictionary = new Dictionary();
// Iterate through the Vote Objects and add each one to the
// lookup table based on it's Aid property.
for each (var vote : Object in votes)
{
// Check for the existance of the 'aId' property to stop
// any 'nulls' getting into our Lookup Table.
if (!vote.hasOwnProperty("Aid")) {
trace("Vote Object did not contain an `Aid` property.");
continue;
}
// Add the entry to the lookup table.
var key : String = vote.Aid;
votesByAid[key] = vote;
}
// You can now use the lookup table to fetch the Vote Objects.
trace(votesByAid[6].Atext); // traces 'i don't know'