如何在 Flex 3 中使用原生 JSON 或 actionjson 解码 Json [英] How to decode Json using native JSON or actionjson in Flex 3

查看:22
本文介绍了如何在 Flex 3 中使用原生 JSON 或 actionjson 解码 Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Json (wf.json)

<代码>{工作流程":{"模板":"分析1",开始":{"实例":"HDA_run1","user":"symtest","date":"3-Mar-2012",时间戳":1330948220475"},主持人":{"name":"bartla",用户":symtest1","密码":"symtest1","安装路径":"",产品":""},javadump":{"pid":"8989","核心文件名":"","堆转储":"",堆":"","JAVA_HOME":""},垫":{},电子邮件":{"to":"ars@gmail.com",主题":"",信息":""},结尾":{}}}

如您所见,有 7 个项目(或主标题 workflow 中的子标题).在每个项目下,它可以有另一组属性,例如:电子邮件 (item) 有 3 个属性 ("name":"value").

因此,根据我需要能够在 Flex 3 UI 中创建控件 (Text) 的属性数量.

我阅读了 类位于根包,所以不需要导入任何东西.您可以在文档中找到有关其用法的信息.>

然而,原生 JSON 仅适用于 Flash Player 11 或更高版本,这意味着您必须至少针对该播放器版本.由于您编译的是 Flex 3 应用程序,因此默认情况下它将针对 Flash Player 9.如果您的要求不禁止您以 FP11+ 为目标,最简单的解决方法是使用 Flex 4.6(或更高版本)SDK 进行编译.您问题中的屏幕截图显示您使用的是 Flex 3.5,因此您必须在构建路径"设置中进行更改.

<小时>

如果你想动态地遍历结果对象,你可以用一个简单的for"循环来实现:

//workflow是你结构的根节点var 工作流:对象 = result.workflow;//迭代'workflow'对象中的键for(var key:工作流中的字符串){跟踪(键 + ':' + 工作流[键]);}//模板:分析1//开始:[对象]//主机:[对象]//...

如果你想递归地做,你可以检查一个值是否是一个对象:

if (workflow[key] is Object) {//也解析那个节点}别的 {//只使用值}

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

解决方案

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
}

这篇关于如何在 Flex 3 中使用原生 JSON 或 actionjson 解码 Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆