2012-01-11 60 views
0

我用var dp:ArrayCollection = new ArrayCollection(container.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 ..

回答

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' 
0

這可以從該對象

for(var i=0;i<data.length;i++){ 
     for(var key in d){ 
     if(d[key] instanceof String) 
      trace(d[key]); 
    } 
} 
1

您可以使用filter()和map()來創建所需數據的新數組。

讓我們假設你已經得到JSON數據到的ArrayCollection(或陣列),所以在這個例子中,我只是創建陣列:

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 

本頁面做了偉大的作業描述地圖,過濾器和其他Array:http://www.onebyonedesign.com/tutorials/array_methods/

+0

雖然Array.filter,Array.map和其他函數Array/Vector方法是非常好吃的,並允許一些不錯的鏈式方法調用 - 它們確實運行得很不錯比他們的mo慢一點基於傳統的基於循環的對應物。 – JonnyReeves 2012-01-19 22:17:13