2011-01-31 116 views
1

我正在使用as3corelib JSON庫並從URLLoader請求中解碼一些JSON。然而,我在與JSON.decode拋出一個錯誤的問題:AS3 JSON.decode拋出#1009錯誤

TypeError: Error #1009: Cannot access a property or method of a null 

object reference. at com.adobe.serialization.json::JSONTokenizer/nextChar() at com.adobe.serialization.json::JSONTokenizer() at com.adobe.serialization.json::JSONDecoder() at com.adobe.serialization.json::JSON$/decode() at Main/drawMap() at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at flash.net::URLLoader/onComplete()

我的代碼如下:

private function storeAssets(e:Event):void 
{ 
    // returned variables from PHP call 
    var variables:URLVariables = new URLVariables(e.target.data); 
    assets = JSON.decode(variables.assets); 
} 

我已經通過我的JSON輸入驗證器,它總是返回爲有效,以便我真的在撓撓我的腦袋。

+0

爲什麼使用URLVariables? 如果您直接從服務器獲取json數據,則不應使用URLVariables,只需獲取響應字符串並將其直接傳遞給JSON.decode – Ben 2011-01-31 01:54:57

回答

0

你在把e.target.data到的URLVariables,按照本例右:http://actionscriptexamples.com/2008/02/27/decoding-url-encoded-strings-in-a-flash-application-using-the-urlvariables-class-in-actionscript-30/

我認爲正在發生的是使用URLVariables是你的整個字符串解碼成一個對象,從而variables.assets不採用JSON格式,因爲它已經被轉換。也可能是因爲variables.assets沒有在返回數據中定義。

跟蹤您的variables.assets並查看它是否爲空或不是JSON格式。

我會使用var variables:URLVariables = new URLVariables(e.target.data)assets = JSON.decode(e.target.data),但不能同時使用兩者。