0

我正在尝试如何在 flex 4.6 中创建自定义项目渲染器并拥有一个 JSON 格式的数据源......我使用以下内容来检索 json 数据并且它可以正常工作,我可以很好地访问数据

myJSONdata[i].id
myJSONdata[i].username   etc...

但是在理解如何使这些数据成为分配给数据组的 dataProvider 的 flex 期望的数据类型(ArrayList、ArrayCollection?)时遇到问题。

public var loader:URLLoader = new URLLoader();
public var jsonContent:URLLoader;
public var myJSONdata:Object;
public var request:URLRequest;

public function Init():void {

request = new URLRequest("URL TO THE JSON DATA...");
loader.load(request);
loader.addEventListener(Event.COMPLETE, jsonLoaded);
}


public function jsonLoaded(event:Event):void {

jsonContent = URLLoader(event.target);      
myJSONdata = JSON.parse(jsonContent.data);  

trace(myJSONdata.length);               

}

当我尝试像这样分配 dataProvider 时......

<s:DataGroup dataProvider="myJSONdata">

我收到此错误:

Initializer for 'dataProvider': values of type mx.collections.IList cannot be represented in text.

我想使用相同的数据访问功能,然后将该数据用作一个数组,然后我可以将其用作 dataGroup 的 dataProvider。

4

2 回答 2

1

要么给你的 DataGroup 和 id,然后像这样在 AS3 中分配 dataProvider:

<s:DataGroup id="myDataGroup">

//in as3

myDataGroup.dataProvider = new ArrayCollection(myJSONdata);

或者您可以像在 MXML 中那样分配它,但必须将变量名包含在花括号中。

<s:DataGroup dataProvider="{myJSONdata}">

您收到该错误的原因是因为 MXML 被myJSONdata视为普通字符串。

您可能仍需要将 JSON 数组放入 ArrayCollection 中,就像我在第一个示例中所做的那样。

希望有帮助。

于 2013-04-06T13:26:19.957 回答
0

虽然这篇文章很旧,但这仍然有用,也许对其他人有用。

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:HTTPService id="jsonLoader" result="jsonLoader_resultHandler(event)"
                   url="http://example.com/sample.json" />
</fx:Declarations>
<fx:Script>
    <![CDATA[
        import com.adobe.serializers.json.JSONDecoder;

        import mx.collections.ArrayCollection;
        import mx.rpc.events.ResultEvent;

        //<s:WindowedApplication .... creationComplete="initApp()">
        public function initApp():void
        {
            //so trigger jsonLoader to load the json data from the link
            // using the HTTPService immediately the application starts;
            jsonLoader.send();
        }

        protected function jsonLoader_resultHandler(event:ResultEvent):void
        {
            var jsonContent:Object = (new JSONDecoder()).decode(event.result.toString());
            //Assuming jsonContent.data is an Array
            resultDataGrid.dataProvider = new ArrayCollection(jsonContent.data);
        }
    ]]>
</fx:Script>
于 2016-01-12T14:53:08.557 回答