JSON的全称是JavaScriptObjectNotation,是一种轻量级的数据交换格式。JSON与XML具有相同的特性,例如易于人编 写和阅读,易于机器生成和解析。但是JSON比XML数据传输的有效性要高出很多。JSON完全独立与编程语言,使用文本格式保存。 JSON数据有两种结构:
•一种就是数组的形式Array。
• 一种就是object对象类型的数据。
下面的例子是一个数组类型的例子。
首先利用json格式构建一个PHP文件
[ { "Id": 7, "Mentions": [ { "Id": 5, "StatusId": 34, "CreatedDateTime":"\/Date(1310051914617+0100)\/", "Text":"Text", "UserName":"Username", "UserLocation":"UK", "UserLanguage":"en-GB", "IsCheckIn":"true" } ], "Checkins": 0, "HereNow": 0, "TimeStamp":"\/Date(1310051914639+0100)\/", "Venue": { "Id": 7, "FoursquareId":"cacbf3bd-f0aa-403d-9f9b-2056b4985ba1", "Name":"Venue Name" } }, { "name":"hahahhahah", "port":[ { "port": 8080, "protocol":"HTTP", "IP":"123.12.06.456" } ] } ]在test.as文件中输入以下代码用来调用上面的php文件:
package{ importflash.display.Sprite; importcom.adobe.serialization.json.JSON; importflash.events.Event; importflash.net.URLLoader; importflash.net.URLRequest; publicclassJsonTestextendsSprite { publicfunctionJsonTest() { varloader:URLLoader =newURLLoader(); loader.load(newURLRequest("json.json")); //这里是你要获取JSON的路径; loader.addEventListener(Event.COMPLETE, decodeJSON); } privatefunctiondecodeJSON(evt:Event):void { varpersons:Array= JSON.decode(URLLoader(evt.target).data); trace( persons[0].Id ); trace(persons[0].Mentions[0].Id); trace(persons[0].Checkins); trace(persons[1].name); trace(persons[1].port[0].IP); } }}你就会看到相应的输出结果:
7
5
0
hahahhahah
123.12.06.456
{"btn": 1, "content": [ { "text2": "卫弗瑞", "text5": "乐趣", "text3": "SWORDUSER", "text1": 5, "text4": 45 }, { "text2": "迎风布阵", "text5": "圣龙骑士团", "text3": "IMPALER", "text1": 6, "text4": 44 }, { "text2": "你的假肢", "text5": "圣龙骑士团", "text3": "DRAGOON", "text1": 7, "text4": 41 }, { "text2": "丨欣欣丨", "text5": "圣龙骑士团", "text3": "SILVERKNIGHT", "text1": 8, "text4": 38 }, { "text2": "查西斯", "text5": "圣龙骑士团", "text3": "DRAGOON", "text1": 9, "text4": 37 }, { "text2": "克拉林顿", "text5": "圣龙骑士团", "text3": "DRAGOON", "text1": 10, "text4": 29 } ], "code": 0}替换掉上述的数组类型就好了。。
在flash中编译一下就好了。。。
原文:http://www.cnblogs.com/duhuo/p/4167645.html