David
David

Reputation: 763

Creating Array from Object in ActionScript

I've got an object which was deserialized from JSON. It looks like this, introspected in FlashBuilder:

Cities //object
   Denver //object, contained within Cities
       description "Mile High City"
       location "Colorado"
   Los Angeles//object, contained within Cities
       description "City of Angels"
       location "California"
   New York //object, contained within Cities
       description "The Big Apple"
       location "New York State"

All I'm trying to do is to create an array of cities, so that I can do something like this: trace(arrCities[i].description); //returns "Mile High City" or "City of Angels" depending on the value of i.

But I can't get at the strings. Among things I've tried, none of which work:

var arrCities:Array = objCities as Array; //doesn't work

And:

  for (var prop:String in objCities);       
  { 
     trace("objCities."+prop+" = "+objCities[prop]);
     trace(prop.description);  //this returns only one random description 
                              //(sometimes "The Big Apple, sometimes "City of Angels" etc.
     arrCities.push(objCities[prop]);

  }

and

  for each (var prop:String in objCities);      
  { 
     trace("objCities."+prop+" = "+objCities[prop]);
     trace(prop.description);  // returns "objCities.[object Object] = undefined"
     arrCities.push(objCities[prop]);

  }

There's got to be a simple solution to this but I can't find it. Any help appreciated.

Thanks.

cities: {
    denver: {
        dateCreated: 0
        description: "Mile High Cty"
        cityCode: "dv"
        cityName: "Denver"
        properties: { }
        title: ""
    }
    newyork: {
        dateCreated: 0
        description: "The Big Apple"
        cityCode: "nyc"
        cityName: "New York"
        properties: { }
        title: ""
    }
    losangeles: {
        dateCreated: 0
        description: "City of Angels"
        cityCode: "la"
        cityName: "Los Angeles"
        properties: { }
        title: ""
    }
}

Upvotes: 0

Views: 207

Answers (2)

shanethehat
shanethehat

Reputation: 15570

var objCities:Object = JSON.decode(objStr);
var arrCities:Array = [];

for each (var prop:Object in objCities)     
{ 
     arrCities.push(prop);
}
trace(arrCities[0].description);

Explaination:

The form for(var foo in bar) sets foo as the name of the current element of bar in the iteration. for each(var foo in bar) sets foo as the property of this element of bar. You want each each inner object, not that object's name, to be inserted in the array, so that you end up with an array of objects like {description:"City of Angels",location:"California"}

Upvotes: 3

Kaken Bok
Kaken Bok

Reputation: 3395

This library includes the facto standard of reading JSON into AS3 Objects: https://github.com/mikechambers/as3corelib. You will need to use it. To develop your own version of a JSON parser is in (probably) no case reasonable.

Upvotes: 0

Related Questions