2012-03-12 23 views
4

解碼我有以下JSON(wf.json)如何使用Json的本地JSON或actionjson Flex 3中

{ 
"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":{ 
    } 
} 
} 

正如你可以看到有(主標題workflow內或副標題)7個項目。在每個項目下,它可以有另一組屬性,例如:電子郵件(item)有3個屬性("name":"value")

因此,根據我需要在我的Flex 3用戶界面中創建控件(Text)的屬性數量。

我讀hereactionjsonas3corelib快5-6倍,但我無法找到任何示例代碼。 actionjson文檔說它的功能與corelib相同,所以我甚至試過import com.adobe.serialization.json.JSON; JSON.decode(rawData),但它無法找到JSON

下面是我的代碼

<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> 

請幫我取name, values如果有的話,從每個項目。謝謝

是不是可以直接從一個對象(非定製),如在jquery完成像json數據?

更新與Flex構建路徑

enter image description here

+1

絕對最快的是原生JSON解析,但您必須能夠定位Flash Player 11. – RIAstar 2012-03-12 12:50:38

+0

@RIAstar:我可以讀取的任何鏈接是否意味着它僅對Flash Player 11及更高版本有效? – abi1964 2012-03-12 12:53:31

+0

http://help.adobe.com/en_US/as3/dev/WS324d8efcab3b0d1e2408f9e3131fddffcfc-8000.html是的,這是FP 11+ – RIAstar 2012-03-12 12:56:10

回答

10

如果跑得最快的分析器是你想要的,那麼你會希望使用本地JSON解析。它的使用是如此簡單:

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

JSON類位於根包,所以不需要輸入任何東西。你可以在docs找到它的用法信息。

但是,本機JSON僅適用於Flash Player 11或更高版本,這意味着您必須至少定位該播放器版本。自編譯Flex 3應用程序以來,默認情況下它將定位到Flash Player 9。如果您的要求不禁止您定位FP11 +,最簡單的解決方法是使用Flex 4.6(或更高版本)SDK進行編譯。問題中的屏幕截圖顯示您使用的是Flex 3.5,因此您必須在「構建路徑」設置中更改它。


如果要動態地遍歷得到的對象,你可以用一個簡單的做「for」循環:

//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 (workflow[key] is Object) { 
    //parse that node too 
} 
else { 
    //just use the value 
} 
+0

感謝您的評論的作品。恐怕我必須堅持使用Flex 3編譯器,因爲我的項目是一樣的。所以下面的評論給了我'Analysis1',用於'Alert.show(obj.workflow.template); \t \t \t \t Alert.show(obj.workflow.length()); \t \t \t \t Alert.show(obj.workflow [1]);'代碼,但稍後警報似乎不起作用。我想像前面提到的那樣獲取所有'keys'&'values',它是動態的,我不知道Json可能包含哪些鍵和值。所以可能是某種遍歷,我會得到所有的密鑰。有沒有辦法?謝謝你,對不起,我是這樣一個noob – abi1964 2012-03-14 11:43:58

+0

@AbhishekSimon我爲我的答案添加了一個動態遍歷的例子。您也可以使用Flex 4.6編譯器編譯Flex3應用程序。你只需要確定你的客戶的要求,讓你的目標FP 11 – RIAstar 2012-03-14 12:17:41

+0

真棒..正是我想要的。感謝非常好的信息。 – abi1964 2012-03-15 05:48:29

0
import com.adobe.serializers.json.JSONDecoder; 

var JSON:JSONDecoder = new JSONDecoder(); 
var result:Object = JSON.decode(JSON_STRING); 

,對我 工作,然後你可以構建新的對象類型或僅僅指剛訪問值要麼

result.template 

result['template'] 

後者有利於需要動態衣被合計/鍵,而不是已知的鍵值

+0

我用'import com.adobe.serialization.json.JSONDecoder; var obj = JSON.decode(event.result.toString()); \t \t \t \t Alert.show(obj.template);'但我給了我空的警報框 – abi1964 2012-03-14 05:20:31

+0

也是它原生的JSON解析? – abi1964 2012-03-14 06:08:47

+1

@AbhishekSimon不,它不是,也不是'actionjson'。這是你想要取代的'as3corelib'。 – RIAstar 2012-03-14 10:38:04

1

以下從RIAstar解決方案之後,這是我做的(兩者的Flex 3.5編譯器& 4.6編譯器代碼)

的Flex 3.5的編譯器使用as3corelib.swc爲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":"[email protected]",  "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)); 

    } 
} 

輸出

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編譯器使用本地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":"[email protected]",  "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)); 

    } 
} 

輸出

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 
2
<?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

{ 「名」: 「dibneg」, 「時代」: 「67」, 「性」: 「女」, 「的ImagePath」: 「kamil.jpg」 }