从数组集合中读取单个数据

发布于 2024-12-26 06:34:14 字数 830 浏览 1 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的她 2025-01-02 06:34:14

您可以使用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

此页面很好地描述了映射、过滤器,以及数组的其余部分: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:

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}];

Now you can use Array.filter to create a new array that only contains elements that have a valid value for the desired field:

//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 );
    }
}

To test the above:

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?

Or, you can use Array.map to create an array of only values for a certain property:

//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 ];
    }
}

You can test the map function above with:

var valuesOfAtext:Array = makeArrayOfProperty( 'Atext' );
trace( 'Values of valuesOfAtext: ' + valuesOfAtext );
//OUTPUT: Values of valuesOfAtext: ,yes,no,i don't know

This page does a great job describing map, filter, and the rest of Array: http://www.onebyonedesign.com/tutorials/array_methods/

归属感 2025-01-02 06:34:14

从上面的输出来看,GetVotesResult 是一个对象数组,您可以使用 for / foreach 循环对其进行迭代,例如:

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 中的字典)以创建查找表基于其中之一按键:

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

From your output above, GetVotesResult is an Array of Objects which you can iterate over using a for / for each loop, eg:

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(", "));

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:

// 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'
月朦胧 2025-01-02 06:34:14

这可以从该对象获取所有字符串

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

this can get all String from that object

for(var i=0;i<data.length;i++){
        for(var key in d){
        if(d[key] instanceof String)
            trace(d[key]);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文