How to decode Json using native JSON or actionjson in Flex 3

前端 未结 4 1445
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 07:03

I have the below Json (wf.json)

{
\"workflow\":{
    \"template\":\"Analysis1\",

    \"start\":{
        \"instance\":\"HDA_run1\",
        \"user\":\"symte         


        
相关标签:
4条回答
  • 2020-12-09 07:36
    <?xml version="1.0" encoding="utf-8"?>
    <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import com.adobe.serialization.json.JSON;       
            [Embed(source="assets/test.json",mimeType="application/octet-stream")]
            private var json_file:Class;
            protected function button1_clickHandler(event:MouseEvent):void
            {           
                trace("Hello from Flex Debugging!");
                var bytes:ByteArray = new json_file();
                var json:String = bytes.readUTFBytes(bytes.length);
                trace(json);            
                var arr:Object = JSON.decode(json);
                for (var keyname:String in arr)
                {
                    trace ( keyname + ": " + arr[ keyname ] );          
                }   
                grid.dataProvider = arr;
            }
        ]]>
    </mx:Script>
    <mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10">
            <mx:columns>
                <mx:DataGridColumn headerText="Name" dataField="name"/>
                <mx:DataGridColumn headerText="Age" dataField="age"/>
            </mx:columns>
        </mx:DataGrid>
        <mx:Button x="538" y="45" label="Get Json" click="button1_clickHandler(event)"/>
    </mx:WindowedApplication>
    

    test.json

    { "name": "dibneg", "age" : "67", "sex": "female", "imagePath": "kamil.jpg" }

    0 讨论(0)
  • 2020-12-09 07:38

    If the fastest parser is what you want, then you'll want use native JSON parsing. Its usage is as simple as this:

    var result:Object = JSON.parse(event.result);
    trace(result.workflow.template);  //traces "Analysis1"
    

    The JSON class is located in the root package, so no need to import anything. You can find information on its usage in the docs.

    However native JSON is only available for Flash Player 11 or higher, which means you'll have to target at least that player version. Since your compiling a Flex 3 application, it will target Flash Player 9 by default. If your requirements don't prohibit you from targeting FP11+, the easiest fix is to compile with the Flex 4.6 (or higher) SDK. The screenshot in your question shows that you're using Flex 3.5, so you'll have to change that in the "build path" settings.


    If you wish to traverse the resulting object dynamically, you can do it with a simple 'for' loop:

    //workflow is the root node of your structure
    var workflow:Object = result.workflow;
    //iterate the keys in the 'workflow' object
    for (var key:String in workflow) {
        trace(key + ': ' + workflow[key]);
    }
    //template: Analysis1
    //start: [Object]
    //host: [Object]
    //...
    

    If you want to do it recursively, you can check whether a value is an Object or not:

    if (workflow[key] is Object) {
        //parse that node too
    }
    else {
        //just use the value
    }
    
    0 讨论(0)
  • 2020-12-09 07:38

    After following solution from RIAstar, this is what I did (both Flex 3.5 compiler & 4.6 compiler code)

    Flex 3.5 compiler using as3corelib.swc for JSON

    import com.adobe.serialization.json.JSON;
    
    private var temp:String ='{"workflow":{ "template":"HeapDumpAnalysis",      "start":{       "instance":"HDA_run1",      "user":"symtest",       "date":"3-Mar-2012",        "timestamp":"1330948220475" },  "host":{        "name":"estilo",        "user":"symtest1",      "password":"symtest1",      "installpath":"",       "product":""    },  "javadump":{        "pid":"8989",       "corefilename":"",      "heapdump":"",      "stack":"",     "INFA_HOME":""  },  "mat":{ },  "email":{       "to":"vsatwik@informatica.com",     "subject":"",       "message":""    },  "end":{ }}}';
    private function test():void
    {
        var obj = JSON.decode(temp);
    
        var workflow:Object = obj.workflow;
        for (var key:String in workflow) {
            trace(key + ': ' + workflow[key] + (key is String) + ", " + (workflow[key] is String));
    
        }
    }
    

    output

    javadump: [object Object]true, false
    template: HeapDumpAnalysistrue, true
    host: [object Object]true, false
    end: [object Object]true, false
    mat: [object Object]true, false
    email: [object Object]true, false
    start: [object Object]true, false
    

    Flex 4.6 compiler using native Json parsing

    private var temp:String ='{"workflow":{ "template":"HeapDumpAnalysis",      "start":{       "instance":"HDA_run1",      "user":"symtest",       "date":"3-Mar-2012",        "timestamp":"1330948220475" },  "host":{        "name":"estilo",        "user":"symtest1",      "password":"symtest1",      "installpath":"",       "product":""    },  "javadump":{        "pid":"8989",       "corefilename":"",      "heapdump":"",      "stack":"",     "INFA_HOME":""  },  "mat":{ },  "email":{       "to":"ars@gmail.com",       "subject":"",       "message":""    },  "end":{ }}}';
    
    private function test():void
    {
        var result:Object = JSON.parse(temp);
        var workflow:Object = result.workflow;
    
        for (var key:String in workflow) {
            trace(key + ': ' + workflow[key] + (key is String) + ", " + (workflow[key] is String));
    
        }
    }
    

    output

    javadump: [object Object]true, false
    mat: [object Object]true, false
    end: [object Object]true, false
    email: [object Object]true, false
    host: [object Object]true, false
    start: [object Object]true, false
    template: HeapDumpAnalysistrue, true
    
    0 讨论(0)
  • 2020-12-09 07:44
    import com.adobe.serializers.json.JSONDecoder;
    
    var JSON:JSONDecoder = new JSONDecoder();
    var result:Object = JSON.decode(JSON_STRING);
    

    that worked for me then you can either construct new object type(s) or jsut access values either

    result.template
    

    or

    result['template']
    

    the latter is good for dynamic valus/keys needed rather than known key values

    0 讨论(0)
提交回复
热议问题