Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

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

I have the below Json (wf.json)

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

    "start":{
        "instance":"HDA_run1",
        "user":"symtest",
        "date":"3-Mar-2012",
        "timestamp":"1330948220475"
    },
    "host":{
        "name":"bartla",
        "user":"symtest1",
        "password":"symtest1",
        "installpath":"",
        "product":""
    },
    "javadump":{
        "pid":"8989",
        "corefilename":"",
        "heapdump":"",
        "stack":"",
        "JAVA_HOME":""  
    },
    "mat":{
    },
    "email":{
        "to":"ars@gmail.com",
        "subject":"",
        "message":""
    },
    "end":{
    }
}
}

As you can see there are 7 items (or sub headings inside main heading workflow). Under each item it can have another set of properties eg: email (item) has 3 properties ("name":"value").

So based on the number of properties I need to be able to create controls (Text) in my Flex 3 UI.

I read here that actionjson is 5-6x faster than the as3corelib, but I am not able to find any example code for it. The actionjson doc says it function the same way as corelib, so I even tried import com.adobe.serialization.json.JSON; JSON.decode(rawData) but it is unable to find JSON.

Below is my code

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
            layout="absolute" minWidth="955" minHeight="600"
            creationComplete="service.send()">

    <mx:Script>
    <![CDATA[

        import mx.controls.Alert;
        import mx.rpc.events.ResultEvent;

        private function onJSONLoad(event:ResultEvent):void
        {
            //get the raw JSON data and cast to String
            var rawData:String = String(event.result);
            //Alert.show(rawData); This prints my JSON String

            var obj:Object = decodeJson(rawData);   
            /*error call to possibly undefined method decodeJson*/
            Alert.show(obj.toString());
        }
    ]]>
    </mx:Script>

    <mx:HTTPService id="service" resultFormat="text"
                url="/cjb/wf.json"
                result="onJSONLoad(event)" />

</mx:Application>

Please help me fetch name, values if any from each item. Thanks

Is it not possible to directly fetch json data from an object (not custom made) like it is done in jquery?

Update with Flex Build Path

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...