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

I have 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, in the main title of the workflow there are 7 elements (or under the headings). Each element can have a different set of properties, for example: email (item) has 3 properties ("name":"value") .

Therefore, based on the number of properties, I should be able to create controls (Text) in my Flex 3 interface.

I read here that actionjson is 5-6 times faster than as3corelib , but I can not find any sample code for it. The actionjson document says it works the same as Corelib, so I even tried import com.adobe.serialization.json.JSON; JSON.decode(rawData) import com.adobe.serialization.json.JSON; JSON.decode(rawData) , but it could not 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 get name, values , if any, in each element. Thanks

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

Update Using Flex Build Path

enter image description here

+4
source share
4 answers

If the fastest parser is what you want, then you need to use your own JSON parsing. Its use is just as simple:

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

The JSON class is in the root package, so nothing needs to be imported. You can find usage information in docs .

However, native JSON is only available for Flash Player 11 or higher, which means that you need to target at least this playerโ€™s version. Since you are compiling the Flex 3 application, it will target Flash Player 9 by default. If your requirements do not prevent you from targeting FP11 +, the easiest fix is โ€‹โ€‹to compile with the Flex 4.6 SDK (or higher). The screenshot in your question shows that you are using Flex 3.5, so you have to change this in the "build path" settings.


If you want to dynamically move the resulting object, you can do this 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 this recursively, you can check if the value is an object or not:

 if (workflow[key] is Object) { //parse that node too } else { //just use the value } 
+10
source
 <?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": "woman", "imagePath": "kamil.jpg"}

+2
source

After executing the solution from RIAstar, this is what I did (both the Flex 3.5 compiler and the 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)); } } 

Exit

 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 analysis

 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)); } } 

Exit

 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 
+1
source
 import com.adobe.serializers.json.JSONDecoder; var JSON:JSONDecoder = new JSONDecoder(); var result:Object = JSON.decode(JSON_STRING); 

which worked for me then you can either build new types of objects, or jsut access values โ€‹โ€‹or

 result.template 

or

 result['template'] 

the latter is useful for dynamic valus / keys values, not known key values

0
source

Source: https://habr.com/ru/post/1400987/


All Articles