JSONテスト takaaki024 forked:1favorite:1lines:35license : All rights reserved modified : 2008-12-19 22:22:14 Embed Tweet <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="652" height="182" backgroundColor="#FFFFFF"> <mx:Script> <![CDATA[ // コンパイル時は corelib.swc が libpath に必要 // ※ ここで入手 → http://code.google.com/p/as3corelib/downloads/list // ※ コンパイル時に埋め込まれるので実行時は不要 import com.adobe.serialization.json.JSON; public function testJSON():void { // JSON文字列をパース var arr:Array = (JSON.decode(txtJsonData.text.replace(/\r/gm, "")) as Array); // オブジェクト風にアクセス txtResult.text = "方法1:\n"; for(var i:int = 0; i < arr.length; i++) { txtResult.text += " " + arr[i].name + "(" + arr[i].age + ") said '" + arr[i].greeting + "'!\n"; } // 連想配列風にアクセス txtResult.text += "\n方法2:\n"; for(i = 0; i < arr.length; i++) { // ※ i は 「for文の中だけ」のスコープでは無いみたい・・ (var をつけるとエラーに) txtResult.text += " " + arr[i]["name"] + "(" + arr[i]["age"] + ") said '" + arr[i]["greeting"] + "'!\n"; } } ]]> </mx:Script> <mx:Button id="btnTest" click="testJSON();" x="10" y="10" label="ボタン"/> <mx:TextArea id="txtJsonData" x="10" y="46" width="363" height="126"> <!-- JSONで受け取る文字列についてメモ --> <!-- ・ 値も要素名は ""(ダブルクォーテーション) で囲う。''(シングルクォーテーション)では不可 --> <!-- ・ 最後の要素の後ろに ,(カンマ) をつけてはダメぽい --> <!-- ・ 改行にCR(\r)が入るとパース出来ないぽい --> <mx:text> <![CDATA[[ {"name": "Michael", "age": 15, "greeting": "Hello"}, {"name": "Taro", "age": 30, "greeting": "Konnichiha"} ] ]]> </mx:text> </mx:TextArea> <mx:TextArea id="txtResult" x="408" y="46" width="234" height="126"/> <mx:Label x="381" y="94" text="→"/> </mx:Application> Code Fullscreen Preview Fullscreen huixie name sort new page view favorite forked pv0 forked from: JSONテスト genms forked:0 favorite:0lines:35 (diff:1)