One of the many new features in AIR 3 (in beta, available on Adobe Labs) is the new native JSON parser. It has always been possible to parse JSON with ActionScript, but AIR 3 provides native JSON support which is faster than ActionScript implementations, and more efficient in terms of memory usage.
The two main things the JSON class can do are:
- Parse JSON strings.
- Turn ActionScript objects into JSON ("stringify").
To learn more about JSON support in AIR 3, check out this short sample news reader (you can also download the FXP file) which uses JSON rather than RSS. The code is so simple, I’ll include the entire thing below:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx=" library://ns.adobe.com/flex/mx" applicationComplete="onApplicationComplete();">
<fx:Script>
<![CDATA[
import flash.globalization.DateTimeFormatter;
import flash.globalization.DateTimeStyle;
import flash.globalization.LocaleID;
import mx.rpc.events.ResultEvent;
private var df:DateTimeFormatter;
private function onApplicationComplete():void
{
// Feed data is stored as a JSON file...
var f:File = File.applicationDirectory.resolvePath("feeds.json");
var fs:FileStream = new FileStream();
fs.open(f, FileMode.READ);
var feedJson:String = fs.readUTFBytes(fs.bytesAvailable);
fs.close();
var feeds:Object = JSON.parse(feedJson);
this.feedTree.dataProvider = feeds;
this.postHtmlContainer.htmlLoader.navigateInSystemBrowser = true;
df = new DateTimeFormatter(LocaleID.DEFAULT, DateTimeStyle.MEDIUM, DateTimeStyle.NONE);
}
private function onTreeClick(e:MouseEvent):void
{
if (!this.feedTree.selectedItem || !this.feedTree.selectedItem.data) return;
feedService.send({v:'1.0',num:'20',q:this.feedTree.selectedItem.data});
}
private function onFeedLoaded(e:ResultEvent):void
{
var result:String = e.result.toString();
var feedData:Object = JSON.parse(result);
var s:String = '<html><body>';
s += '<h1>Posts for ' + feedData.responseData.feed.title + '</h1>';
for each (var post:Object in feedData.responseData.feed.entries)
{
s += '<p class="postTitle"><a href="' + post.link + '">' + post.title + ' »</a></p>';
s += '<p>' + df.format(new Date(post.publishedDate)) + '</p>';
s += '<p>' + post.content + '</p><hr/>';
}
s += '</body></html>';
this.postHtmlContainer.htmlText = s;
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService id="feedService" url="https://ajax.googleapis.com/ajax/services/feed/load" result="onFeedLoaded(event)" resultFormat="text" contentType="application/x-www-form-urlencoded" method="GET" showBusyCursor="true" />
</fx:Declarations>
<mx:HDividedBox width="100%" height="100%">
<mx:Tree id="feedTree" width="200" height="100%" click="onTreeClick(event);"/>
<mx:HTML width="100%" height="100%" id="postHtmlContainer"/>
</mx:HDividedBox>
</s:WindowedApplication>
Keep in mind that AIR 3 is still in beta, so you might find bugs. If you do, here’s how to file them.

Nice, but I wonder why it’s in AIR only.
It will be in the Flash Player, as well. I should have mentioned that.
Pingback: webGame开发|网页游戏开发|flex教程|AS3.0教程|flex企业应用|AIR » Blog Archive » [业界新闻]AIR 3对JSON的原生支持
Pingback: Cool Stuff with the Flash Platform - 8/19/2011 | Remote Synthesis
I wonder why not BSON.
@Vic It was named after Jason from Friday The 13th! o_O
If it comes to Flash Player that is good to hear! Because otherwise there’s a name conflict with the adobe corelib one right now that gives you compile-time errors.
First, apologies for cross posting this (I originally added a comment on your Google+ post), but I figure this is the more appropriate spot…
Hi Christian, I am trying to use native JSON with Air 3 RC1. I am able to compile against the SDK no problem, but at runtime I get: “ReferenceError: Error #1065: Variable JSON is not defined.” I triple checked that I am using the correct SDK, and stragely FlashBuilder recognizes and code-hints “JSON” when referenced in the IDE. Any ideas? Do I need to add a compilation flag for the Flex compiler or something?
Hi Shaun,did you figure it out?I got the same error.
Pingback: Slides, Links, and Questions From my MAX 2011 Presentation « Christian Cantrell
I also get a “Error #1065: Variable JSON is not defined.” in runtime.
Here’s the solution to get JSON working in Adobe Air 3 projects:
http://forums.adobe.com/message/3933890
“Hi,guys,at last i found the solution in the post “http://blogs.adobe.com/cantrell/archives/2011/08/how-to-use-the-air-3- beta-sdk.html” 。Just go to “Project Properties,” then “Flex Compiler.” In the “Additional compiler arguments” box, add “-swf-version=13″ (13 represents air3)”
I also get a “Error #1065 too
Native JSON – is very good. BUT for me very critical backward compatibility with old versions of AIR and FLEX sdk’s. At the moment I’ve got errors like
Error: Can not resolve a multiname reference unambiguously. JSON (from …\frameworks\libs\air\airglobal.swc(JSON, Walker)) and com.adobe.serialization.json:JSON (from ..\src\com\adobe\serialization\json\JSON.as) are available.
How should I resolve it?
Pingback: JSON – tors 24/11-2011 « FLA11
gör bak nası döwüyom
I had to download the latest version of the Flex SDK (4.6) from here:
http://www.adobe.com/devnet/flex/flex-sdk-download.html
Then right click on the project, go to properties, then go to Flex Compiler and set the SDK to 4.6.
Then I had to change this tag in the application manifest from:
to
No errors after that. I kinda like Flex / Air / Flash Builder, but documentation needs a lot more work. Adobe should take PHP’s documentation website as an example.
The changes in the manifest are in the application tag change xmlns from:
http://ns.adobe.com/air/application/2.6
to
http://ns.adobe.com/air/application/3.1
Pingback: AIR3.0新特性 | augustli's blog
You can find here a solution that compiles for both version of player, using internal JSON when available.
It compiles transparently without triggering any error:
http://www.pippoflash.com/index.php/2012/06/20/flash-player-10-and-flash-player-11-json-json-conflict-solved/
Find the solution for thr error Cannot Resolve a Multiname Reference Unambiguously JSON
in the following URL
http://www.smarttutorials.net/cannot-resolve-a-multiname-reference-unambiguously-json-in-flash-builder-4-6-with-smartfox-server/