I am accessing it in Flex with the following code:
<mx:Script>
private function onJSONLoad(event:ResultEvent):void
{
var rawData:String = String(event.result);
var countries:Array = (JSON.decode(rawData) as Array);
Alert.show(countries.length as String);
var dp:ArrayCollection = new ArrayCollection(arr);
However, for the life of me, I cannot get Flex to correctly parse the JSON code into an array. In the line:
var countries:Array = (JSON.decode(rawData) as Array);
The countries array never gets populated with any of the data from the JSON file, even though the contents of the rawData variable correctly contain all the JSON code. And whenever I try to access the countries array, I receive the following error message:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
What am I doing wrong? Can someone point me in the right direction? Is there a problem with the JSON code structure? Is there something remarkably stupid I am overlooking? Hope someone can help me look at this with a fresh pair of eyes and see if they can spot the problem, because I have really hit a wall here.
Help me Obi-wan, you're our only hope!! I have been working on this problem for the past two days and really don't know where else to turn to for help!
Many thanks!
Phil
Hi stinkytofu, did you find a solution to this as i am having the same problem. turning into an array is bringing up an error..
cheers
chris
You don't need to cast it to an array, really. Here is how I did it:
[Bindable] private var dp:ArrayCollection = new ArrayCollection();
[Bindable] private var dp2:ArrayCollection = new ArrayCollection();
private function onLoad():void
{
//trace('sending service call');
service.send();
}
private function onJSONLoad(event:ResultEvent):void
{
//trace('got a result from service');
var countries:Object = JSON.decode(event.result as String);
for(var i:String in countries['states'])
{
dp.addItem(countries['states'][i]);
}
for(var j:String in countries['USA'])
{
var temp:Object = new Object();
temp.name = j;
temp.value = countries['USA'][j];
dp2.addItem(temp);
}
}