0

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: ""
    }
}
2
  • 2
    If you can share the JSON, it will probably be easier to answer. Commented Jul 31, 2011 at 12:19
  • JSON added in the most recent edit above. Thanks. Commented Jul 31, 2011 at 13:17

2 Answers 2

3
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"}

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks -- but that doesn't work either. It gives me arrCities.length = 1. Sometimes it returns the New York object, sometimes LA, sometimes Denver. But only 1.
Then could you please post the original JSON string, and tell us what you're using to deserialize it? I'll need to properly recreate your object to see why this fails.
@David - stupid typo my my part, shouldn't be a semi-colon on the first line! The semi-colon terminates the loop, and then the code in braces only executes once for the first object property.
Yes!! You're a hero. Interesting (I did not know this) that within a for..in loop you don't use a semicolon. Last piece: What if I want to access something like description or cityCode?
You can use it inside the loop, just not where I did before my edit: for each() ; {
|
0

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.

5 Comments

Yes, I'm already using it. I've pulled in the JSON and converted it to objects. Now I need to get at the data in the objects. That's where I'm stuck.
Great, perhaps the cities are not a list and the JSON parser returns you an object instead of an enumerated array. Is this right? trace (this what json parser returns);
Than you access your cities either via loop over the object properties (for in) or - if you know the cities - directly using the citiy as key: jsonReturnObj[myCityName][description].
Thanks but I've tried that as shanethehat suggested and it returns one item. I then tried a nested loop, ie looping through each city, but that also didn't work. Looks like I'll have to post some JSON....
I've posted the JSON. By the way I don't know the city names. What I'm trying to do is populate a combobox with them. The JSON changes, listing different cities according to various criteria, so I need to dynamically read in the city names from the JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.