2010-09-13 29 views
2

所以我有一個包含幾個節點的JSON鏈接(不確定你在JSON中稱他們是什麼),我需要在ActionScript中放入數組,但我仍然無法找到所有特定的節點內容。如何獲取JSON數據並將其推送到數組中? [AS3]

我發現了一個similar question here,但the fix只是展示瞭如何描繪出整個JSON文件(顯示在我的輸出窗口爲[對象對象],[目標對象],[目標對象],...)

第一個節點:{"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數組中,與持續時間數據相同。

任何提示?

回答

2

您的循環錯誤使用3次變量i以訪問下面三型動物Object(englishCaptionsObject,內容和開始時間):

編輯:我假設你的第一個解碼已經是不錯的,但事實並非如此, 你的字幕Array是在你的JSON數據,內容的標題字段,開始時間不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++; 
} 
+0

在第一道,我得到[對象對象],[物體Obj ect],...第二個跟蹤我得到空和第三個拋出一個錯誤:(TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。你知道是否有方法在Flash輸出窗口中看到實際的字符串內容? – 2010-09-14 08:47:30

+0

@leon我已經修改了答案,你沒有得到正確的字段 – Patrick 2010-09-14 10:30:36

+0

啊,這是它的甜蜜!謝謝 :) – 2010-09-14 17:09:08

相關問題