簡體   English   中英

無法在Flex中解碼JSON數據

[英]Not able to decode JSON data in Flex

我是Flex的新手。 我正在嘗試使用以下代碼將JSON數據填充到Datagrid中。 但是我無法解碼JSON數據。 我在我的flex項目中添加了as3corelib.swc。 我試圖從Java后端填充JSON數據,但失敗並顯示以下錯誤(在頁面末尾給出)。 因此,嘗試使用此簡單示例來了解JSON解碼器是否有效。 我已經提到了很多互聯網資源,並且以下代碼似乎有效。

在此方面尋求您的寶貴指導。

private function applicationCompleteHandler():void
{
    var channel:AMFChannel = new AMFChannel("my-amf", "hxxp://localhost:8400/springapp/messagebroker/amf");
    var channelSet:ChannelSet = new ChannelSet();
    channelSet.addChannel(channel);
    po.channelSet = channelSet;
    //po.findAll();

    var httpService:HTTPService=new HTTPService();
    httpService.resultFormat="text";
    httpService.url="http://date.jsontest.com/?service=ip"
    httpService.method=HTTPRequestMessage.POST_METHOD;
    httpService.contentType="application/json";
    httpService.send();
    httpService.addEventListener(ResultEvent.RESULT, onJSONLoad);
}

private function onJSONLoad(event:ResultEvent):void
{
    var rawData:String=String(event.result);
    var arr:Array=(com.adobe.serialization.json.JSON.decode(rawData) as Array);
    var dp:ArrayCollection=new ArrayCollection(arr);
    dg.dataProvider=dp;

}

JSONParseError: Unexpected o encountered
at com.adobe.serialization.json::JSONTokenizer/parseError()
at com.adobe.serialization.json::JSONTokenizer/getNextToken()
at com.adobe.serialization.json::JSONDecoder/nextToken()
at com.adobe.serialization.json::JSONDecoder/parseArray()
at com.adobe.serialization.json::JSONDecoder/parseValue()
at com.adobe.serialization.json::JSONDecoder()
at com.adobe.serialization.json::JSON$/decode()
at ProductService/po_resultHandler()
at ProductService/__po_result()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc::AbstractService/dispatchEvent()
at mx.rpc.remoting.mxml::RemoteObject/dispatchEvent()
at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()
at mx.rpc::Responder/result()
at mx.rpc::AsyncRequest/acknowledge()
at NetConnectionMessageResponder/resultHandler()
at mx.messaging::MessageResponder/result()

如下所示,而不是使用數組使用對象

private function onJSONLoad(event:ResultEvent):void
{
   var jsonObj:Object= com.adobe.serialization.json.JSON.decode(urlLoader.data);
   trace(jsonObj.forecastList[0].dayDesc);              

}

我有不同的json格式,因此請使用您的json格式來使對象停頓,或者向我顯示您的json格式或URL。

如果您沒有問題,則可以使用urlRequest和urlLoader而不是使用httpservice,這樣會更好。

感謝Subrat和Brian。

我認為問題出在JSON輸入格式上。 Java正在以對象結構發送JSON數據。 因此,我找到了一個提供樣本JSON數據的網站。 然后在調試時,我發現網站以純文本格式共享數據,這與我的Java后端發送的內容完全不同。 代碼如下。

private function onJSONLoad(event:ResultEvent):void

{

var jsonObj:Object = (JSON.parse(event.result.toString()));
var arr:ArrayCollection = new ArrayCollection(jsonObj.heroes as Array);
var arrobj:ArrayCollection = new ArrayCollection(jsonObj as Array);
this.dg.dataProvider = new ArrayCollection(jsonObj.heroes as Array);

}

<s:HTTPService id="httpService" 
url="https://raw.githubusercontent.com/kronusme/dota2-api/master/data/heroes.json"
resultFormat="text" method="POST" useProxy="false" result="onJSONLoad(event)"/>

盡管我在運行此代碼時遇到跨域問題,但是我仍然可以調試此代碼,並且數據按預期填充在我的datagrid中。

因此,我可能會嘗試修復從Java后端發送到UI的格式。

我在后端使用net.sf.json創建JSONArray。 如果這不起作用,我將在Flex中使用RemoteObject並設法填充我的datagrid :(

再次感謝你們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM