在Flex中使用JSON新手入门: 读取豆瓣API

Categories: Flex; Tagged with: ; @ December 13th, 2012 17:59

豆瓣提供了丰富的API, 在Flex中可使用WebService快速调用。

读取指定用户收藏过的图书:

image

 

在Flex中使用JSON

FlashPlayer 11开始原生支持JSON:

Native JSON (JavaScript Object Notation) Support — Allows ActionScript developers to take advantage of high performance native parsing and generation of JSON-formatted data.

http://helpx.adobe.com/x-productkb/multi/release-notes-flash-player-11.html

在Flex SDK 4.6中已有顶级类: JSON。

如果使用旧的SDK, 则可以as3corelib:

An ActionScript 3 Library that contains a number of classes and utilities for working with ActionScript? 3. These include classes for MD5 and SHA 1 hashing, Image encoders, and JSON serialization as well as general String, Number and Date APIs.

不过看起来已经有两年多没有更新了。 swc下载地址:  https://github.com/mikechambers/as3corelib/downloads

SDK4.6与As3CoreLib中JSON Class的method name略有不同。

代码:

			private function onJSONLoad(event:ResultEvent):void
			{
				var rawData:String = String(event.result);
				var rawResult:Object = JSON.parse(rawData);
				var books:Array = (rawResult.collections as Array);
				
				var dp:ArrayCollection = new ArrayCollection(books);
				dataGridBooks.dataProvider = dp;
			}

在GitHub上查看完整代码:https://github.com/DavidGuoliang/DoubanAnalytics/blob/master/src/DouBanJson.mxml

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.