如何JSON序列化fx:模型

时间:2012-05-04 17:05:40

标签: json flex actionscript flex4

我正在使用fx:Model进行测试,但将其序列化为JSON却无法正常工作。在下面的例子中,我没有得到JSON中的模型数据,我得到的只是uuid。如何干净地将模型序列化为JSON?

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;

        protected function application1_creationCompleteHandler(event:FlexEvent):void
        {
            mod1.part = "Initial value";
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <fx:Model id="mod1">
        <data>
            <part>{ti.text}</part>
        </data>
    </fx:Model>

</fx:Declarations>
<s:TextInput id="ti" x="98" y="155" text="{mod1.part}">
</s:TextInput>
<s:Button x="120" y="193" label="Read from model" click="Alert.show(mod1.part, 'Model Data')"/>
<s:Button x="120" y="220" label="Model as JSON" click="Alert.show(JSON.stringify(mod1), 'Model as JSON')"/>

1 个答案:

答案 0 :(得分:1)

我可以通过将stringify调用更改为...

将数据序列化为JSON
JSON.stringify(mod1.valueOf())