如何使用本地JSON或actionjson在Flex

如何使用本地JSON或actionjson在Flex

本文介绍了如何使用本地JSON或actionjson在Flex 3的脱$ C C的Json $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下JSON(wf.json)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":"[email protected]", "subject":"", "message":"" }, "end":{ }}}正如你可以看到有7个项目(或子标题主标题内部工作流程)。在每个项目可以有另外一组属性,例如:电子邮件(项目)有3个属性(名:值)。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").因此​​,基于属性的号码,我需要能够在我的Flex 3的UI (文字)创建控件。So based on the number of properties I need to be able to create controls (Text) in my Flex 3 UI.我读此处的 actionjson 是5-6x比快的as3corelib ,但我无法找到任何例如code吧。该actionjson医生说是功能相同的方式corelib的,所以我甚至试过进口com.adobe.serialization.json.JSON; JSON.de code(RAWDATA)但无法找到 JSON 。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.下面是我的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这难道不是可以直接从一个对象(不定制)获取JSON数据像它在的 jQuery的?Is it not possible to directly fetch json data from an object (not custom made) like it is done in jquery? 更新与Flex构建路径推荐答案如果最快的分析器是你想要什么,那么你会希望使用原生JSON解析。它的用法很简单,只要这样的: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"借助 JSON 类位于根包,所以不需要输入任何东西。你可以找到它在docs.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.不过本地JSON仅适用于Flash播放器11或更高,这​​意味着你必须至少指定的播放器版本。由于您编译Flex 3应用程序时,将针对Flash Player的9默认情况下。如果你的要求不从定位FP11 +禁止你,最简单的解决方法是进行编译的Flex 4.6(或更高版本)的SDK。在你的问题的屏幕截图显示你正在使用Flex 3.5,所以你必须要改变,在构建路径设置。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.如果您要动态地遍历得到的对象,你可以做一个简单的for循环: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 structurevar workflow:Object = result.workflow;//iterate the keys in the 'workflow' objectfor (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} 这篇关于如何使用本地JSON或actionjson在Flex 3的脱$ C C的Json $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 13:20