Reputation: 39018
so I have a JSON link that contains a couple of nodes (not sure what you call them in JSON) that I need to put into arrays in ActionScript, but I'm still having trouble trying to trace out all the specific node content.
I found a similar question here, but the fix just showed how to trace out the entire JSON file (which shows up in my output window as [object Object],[object Object],[object Object],...)
first node: {"captions":[{"content":"[hello world] ","startTime":0,"duration":4000}
My code:
private function onCaptionsComplete( event:Event ):void
{
//var jsonObj:Object = JSON.decode(event.target.data);
//var englishCaptionsObject = jsonObj;
var englishCaptionsObject:Object = JSON.decode(cc_loader.data);
captionsContent.push(englishCaptionsObject);
trace("captionsContent = "+captionsContent);
for (var i:String in englishCaptionsObject)
{
trace(i + ": " +englishCaptionsObject[i]);
trace(i + ": " +englishCaptionsObject[i].content);
trace(i + ": " +englishCaptionsObject[i].content[i]);
trace(i + ": " +englishCaptionsObject[i].startTime[i]);
}
}
When I run it here are my traces below:
captionsContent = [object Object]
captions: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
captions: undefined
TypeError: Error #1010: A term is undefined and has no properties.
What I would like to be able to do is put the content data into the captionsContent Array, and the startingTime into a startingTime Array, same with the duration data.
Any tips?
Upvotes: 2
Views: 4856
Reputation: 15717
You have an error in your loop you use 3 times the variable i
to access three differents Object
(englishCaptionsObject, content and startTime) :
Edit : i have assumed that your first decode was already good, but that is not the case,
Your captions Array
is in the captions field of your json data, content and startTime are not Array
s :
// get the captions array
var captions:Array=englishCaptionsObject.captions;
var cnt:int=0;
// loop throug each caption
for each (var caption:Object in captions) {
var content:String = caption.content;
trace(cnt+" "+content);
var startTime:Number = Number(caption.startTime);
trace(cnt+" "+startTime);
cnt++;
}
Upvotes: 2