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

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

I have the below Json (wf.json)

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

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


        
4条回答
  •  -上瘾入骨i
    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
    }
    

提交回复
热议问题