Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Contributor
Posts: 13
Registered: ‎01-18-2011
My Device: bold 9000
My Carrier: telkomsel
Accepted Solution

JSON Parser

Hi all Robot Happy

 

I have some problem with Json parser.

I have download and insert lib file (as3corelib.swc).

 

then I tried to use some code to run it.

 

 

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:mx="library://ns.adobe.com/flex/mx"
		xmlns:fx="http://ns.adobe.com/mxml/2009"
		xmlns:s="library://ns.adobe.com/flex/spark"
		initialize="init()">
	
	<fx:Script>
		<![CDATA[
			import com.adobe.serialization.json.JSON;
			
			// var ini
			// ------------------------------------------------------------------------
			private var filePath:String = "assets/data.txt";
			private var urlLoader:URLLoader;
			private var jsonDataArray:Array;
			// ------------------------------------------------------------------------
			
			
			// ------------------------------------------------------------------------
			private function init():void
			{
				// Add event listener for button click
				btn.addEventListener(MouseEvent.CLICK,loadJSONFile);
			}
			// ------------------------------------------------------------------------
			
			
			
			
			// ------------------------------------------------------------------------
			private function loadJSONFile(e:MouseEvent=null):void
			{
				// Load file
				urlLoader = new URLLoader();
				urlLoader.addEventListener(Event.COMPLETE, fileLoaded,false,0,true);
				//urlLoader.addEventListener(IOErrorEvent.IO_ERROR, fileLoadFailed);
				urlLoader.load(new URLRequest(filePath));
			}
			// ------------------------------------------------------------------------
			
			
			
			// ------------------------------------------------------------------------
			private function fileLoaded(e:Event):void
			{
				// Clean up file load event listeners
				urlLoader.removeEventListener(Event.COMPLETE, fileLoaded);
				
				// If you wanted to get the data from the event use the line below
				//var urlLoader:URLLoader = URLLoader(event.target);
				
				// Parse the file to an array
				jsonDataArray = JSON.decode(urlLoader.data);
				
				// Proceed to do something with the loaded data
				proceed();
			}
			// ------------------------------------------------------------------------
			
			
			
			
			// ------------------------------------------------------------------------
			private function proceed():void
			{
				// Retrieve data tests
				trace("jsonDataArray[0].name = " + jsonDataArray[0].name);
				
				// Populate data grid with our JSON Data
				dg.dataProvider = jsonDataArray;				
			}
			// ------------------------------------------------------------------------
			
			
			
		]]>
	</fx:Script>
	
	<s:DataGrid horizontalCenter="0" top="100" id="dg" width="400" height="200">
		<s:columns>
			<s:ArrayList>
			<s:GridColumn headerText="Product" dataField="product"/>
			<s:GridColumn headerText="Price" dataField="price"/>
			</s:ArrayList>
		</s:columns>
	</s:DataGrid>
	<s:Label text="An example of parsing JSON using AS3Corelib" color="#FFFFFF" fontWeight="bold" left="10" top="10" fontSize="14"/>
	<s:Button label="PARSE DATA INTO DATA GRID" horizontalCenter="0" top="70" width="400" id="btn"/>
	
</s:View>

err.JPG  here the error.

do i need to change data grid? cause i want to use list(maybe)

 

can any1 point me with simpler json parser?

thks b4 Smiley Happy

 

 

 

 

 

Highlighted
Contributor
Posts: 34
Registered: ‎01-05-2011
My Device: Playbook
My Carrier: Sprint

Re: JSON Parser

Try converting your jsonDataArray to an ArrayCollection type:

 

var arrayCollection:ArrayCollection = new ArrayCollection();

arrayCollection.source = jsonDataArray;

dg.dataProvider = arrayCollection;

Contributor
Posts: 13
Registered: ‎01-18-2011
My Device: bold 9000
My Carrier: telkomsel

Re: JSON Parser

well its still returns an error, but I get ur point by using ArrayCollection though. Smiley Happy