【问题标题】:How to obtain and push JSON data into Arrays? [AS3]如何获取 JSON 数据并将其推送到数组中? [AS3]
【发布时间】:2011-04-11 20:07:21
【问题描述】:

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

我找到了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数组,将startingTime放入startingTime数组,与时长数据相同。

有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

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

    编辑:我假设您的第一个解码已经很好,但事实并非如此, 你的标题Array在你的json数据的标题字段中,内容和开始时间不是Arrays:

    // 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++;
    }
    

    【讨论】:

    • 在第一个跟踪中我得到 [object Object],[object Object],... 第二个跟踪我得到 null 并且第三个抛出一个错误:( TypeError: Error #1009: Cannot access a空对象引用的属性或方法。您知道是否有办法在 Flash 输出窗口中查看实际的字符串内容?
    • @leon 我已经修改了你没有得到正确字段的答案
    • 啊,就是这样!谢谢:)
    猜你喜欢
    • 2013-10-13
    • 2015-03-31
    • 1970-01-01
    • 2021-02-06
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多