如何获取JSON数据并将其推入数组? [AS3]

时间:2010-09-13 19:04:09

标签: flash arrays actionscript-3 json for-loop

所以我有一个JSON链接,其中包含我需要在ActionScript中放入数组的几个节点(不确定你在JSON中称它们是什么),但我仍然无法追踪所有特定节点内容。

我找到了similar question here,但是the fix只展示了如何追踪整个JSON文件(在我的输出窗口中显示为[object Object],[object Object],[object Object] ],...)

第一个节点:{"captions":[{"content":"[hello world] ","startTime":0,"duration":4000}

我的代码:

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

当我在这里运行它时,我的痕迹如下:

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.

我希望能够做的是将内容数据放入captionsContent Array,将startingTime放入startingTime数组,与持续时间数据相同。

任何提示?

1 个答案:

答案 0 :(得分:2)

循环中出现错误,您使用变量i的3倍来访问三个不同的Object(englishCaptionsObject,content和startTime):

编辑:我认为您的第一次解码已经很好了,但事实并非如此, 您的标题Array位于json数据的标题字段中,content和startTime不是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++;
}