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: 40
Registered: ‎04-26-2011
My Device: blackberry playbook
My Carrier: Airtel

CANNOT PARSE JSON FILE URGENT...........

THE SAMPLE CODE FOR PARSING THE JSON IS THIS::

 

<?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"
                       creationComplete="service.send();">
    <fxSmiley Very Happyeclarations>
        <s:HTTPService id="service" resultFormat="text"
                       url="assets/FFA_JSON.json"
                       result="onJSONLoad(event)" fault="onJSONFault(event)"/>
    </fxSmiley Very Happyeclarations>
    

    <fxSmiley Frustratedcript>
        <![CDATA[
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.utils.ObjectUtil;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.collections.ArrayCollection;
            import com.adobe.serialization.json.JSON;
            
            private function onJSONLoad(event:ResultEvent):void
            {
                //get the raw JSON data and cast to String    
                var rawDataSmiley Frustratedtring = String(event.result);
                
                //decode the data to ActionScript using the JSON API
                //in this case, the JSON data is a serialize Array of Objects.
                var arr:Array = (JSON.decode(rawData) as Array);
                
                //obtain array elements schema using ObjectUtil
                var schemaSmiley Surprisedbject = ObjectUtil.getClassInfo(arr[0]);
                
                for each(var tokenSmiley Surprisedbject in schema.properties) {
                    var dataSmiley Frustratedtring = token.toString();
                    var colSmiley Very HappyataGridColumn = new DataGridColumn(data);
                    col.dataField = data;
                    grid.columns.push(data);
                }
                
                //create a new ArrayCollection passing the de-serialized Array
                //ArrayCollections work better as DataProviders, as they can
                //be watched for changes.
                var dp:ArrayCollection = new ArrayCollection(arr);
                
                //pass the ArrayCollection to the DataGrid as its dataProvider.
                grid.dataProvider = dp;
            }
            
            private function onJSONFault(evt:FaultEvent):void {   
                trace(evt.message);   
            }        
        ]]>
    </fxSmiley Frustratedcript>
    
    <mxSmiley Very HappyataGrid id="grid" right="10" left="10" top="10" bottom="10"/>
    
</s:WindowedApplication>

 

 

 

AND THE JSON FILE IS::

 

 

{
  "name": "34.0585213,-118.4418411",
  "Status": {
    "code": 200,
    "request": "geocode"
  },
  "Placemark": [ {
    "id": "p1",
    "address": "10866 Wilshire Blvd, Los Angeles, CA 90024, USA",
    "AddressDetails": {
   "Accuracy" : 8,
   "Country" : {
      "AdministrativeArea" : {
         "AdministrativeAreaName" : "CA",
         "Locality" : {
            "LocalityName" : "Los Angeles",
            "PostalCode" : {
               "PostalCodeNumber" : "90024"
            },
            "Thoroughfare" : {
               "ThoroughfareName" : "10866 Wilshire Blvd"
            }
         }
      },
      "CountryName" : "USA",
      "CountryNameCode" : "US"
   }
},
    "ExtendedData": {
      "LatLonBox": {
        "north": 34.0617166,
        "south": 34.0554214,
        "east": -118.4388334,
        "west": -118.4451286
      }
    },
    "Point": {
      "coordinates": [ -118.4419810, 34.0585690, 0 ]
    }
  } ]
}

 

THE ERROR BEING SHOWN IS::

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Json4th/onJSONLoad()[C:\Users\399024\Adobe Flash Builder Burrito Preview\FFATrial\Json4th\src\Json4th.mxml:32]
    at Json4th/__service_result()[C:\Users\399024\Adobe Flash Builder Burrito Preview\FFATrial\Json4th\src\Json4th.mxml:7]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\http\HTTPService.as:993]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:318]
    at mx.rpc::Responder/result()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]
    at mx.rpc::AsyncRequest/acknowledge()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:84]
    at DirectHTTPMessageResponder/completeHandler()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:451]
    at flash.events::EventDispatcher/dispatchEventFunction()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Highlighted
Developer
Posts: 229
Registered: ‎08-14-2009
My Device: Bold 9900, DevAlpha A
My Carrier: Vodafone DE

Re: CANNOT PARSE JSON FILE URGENT...........

[ Edited ]

Hey you might want to check if this has something to do with your CAPSLOCK key beeing broken. Additionally there is an "Insert Code" Button to paste code into your posts which would make it a lot easier to help you because like this the only thing I can see is random smilies ^^.

Willy R.
-
If my post solved your problem, make it easier for others to find by Accepting it as a solution or provide a Kudo if it was helpful for you. Thanks.
Contributor
Posts: 40
Registered: ‎04-26-2011
My Device: blackberry playbook
My Carrier: Airtel

Re: CANNOT PARSE JSON FILE URGENT...........

 no problem with my capslock key...the parser code works with this json files::

 

[
    {
        "vehicleType": "excavator",
        "vehicleColor": "yellow",
        "fuel": "diesel",
         "treadType": "plate chain"
    },
    {
        "vehicleType": "Dump Truck",
        "vehicleColor": "yellow",
        "fuel": "gasoline",
        "treadType": "wheel"
    },
    {
        "vehicleType": "Grader",
        "vehicleColor": "green",
        "fuel": "diesel",
        "treadType": "wheel"
    }
   
   ]

 

 

 

 

NOTICE--WHEN I REMOVE THE SQUARE BRACKETS THE ERROR SHOWS UP.OTHERWISE IT WORKS FINE.

Developer
Posts: 229
Registered: ‎08-14-2009
My Device: Bold 9900, DevAlpha A
My Carrier: Vodafone DE

Re: CANNOT PARSE JSON FILE URGENT...........

Again, please use the "Insert Code" Button to paste code into your posts. Also we can all read and theres no need to write in capitalized letters which i consider to be "shouting".  It'd be helpfull if you provided a little more information on what excatly you are trying to do and what your problem is. 

Willy R.
-
If my post solved your problem, make it easier for others to find by Accepting it as a solution or provide a Kudo if it was helpful for you. Thanks.
Contributor
Posts: 40
Registered: ‎04-26-2011
My Device: blackberry playbook
My Carrier: Airtel

Re: CANNOT PARSE JSON FILE URGENT...........

i am trying to parse a local json file in a flex desktop application and display its contents in a datagrid....the column property of the datagrid changes according to the json file..but whenever i remove the array notation in the json file the error shown above creeps up....i.e.,the code will parse the folowing json file:

[
    {
        "vehicleType": "excavator",
        "vehicleColor": "yellow",
        "fuel": "diesel",
         "treadType": "plate chain"
    },
    {
        "vehicleType": "Dump Truck",
        "vehicleColor": "yellow",
        "fuel": "gasoline",
        "treadType": "wheel"
    },
    {
        "vehicleType": "Grader",
        "vehicleColor": "green",
        "fuel": "diesel",
        "treadType": "wheel"
    }
   
  ] 

 

but not this file:

    {
        "vehicleType": "excavator",
        "vehicleColor": "yellow",
        "fuel": "diesel",
         "treadType": "plate chain"
    },
    {
        "vehicleType": "Dump Truck",
        "vehicleColor": "yellow",
        "fuel": "gasoline",
        "treadType": "wheel"
    },
    {
        "vehicleType": "Grader",
        "vehicleColor": "green",
        "fuel": "diesel",
        "treadType": "wheel"
    }
   
  

 

the source code for my parser is:

<?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"
					   creationComplete="service.send();">
	<fx:Declarations>
		<s:HTTPService id="service" resultFormat="text"
					   url="assets/geo11.json"
					   result="onJSONLoad(event)" fault="onJSONFault(event)"/>
	</fx:Declarations>
	

	<fx:Script>
		<![CDATA[
			import mx.controls.dataGridClasses.DataGridColumn;
			import mx.utils.ObjectUtil;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.collections.ArrayCollection;
			import com.adobe.serialization.json.JSON;
			
			private function onJSONLoad(event:ResultEvent):void
			{
				//get the raw JSON data and cast to String    
				var rawData:String = String(event.result);
				
				//decode the data to ActionScript using the JSON API
				//in this case, the JSON data is a serialize Array of Objects.
				var arr:Array = (JSON.decode(rawData) as Array);
				
				//obtain array elements schema using ObjectUtil
				var schema:Object = ObjectUtil.getClassInfo(arr[0]);
				
				for each(var token:Object in schema.properties) {
					var data:String = token.toString();
					var col:DataGridColumn = new DataGridColumn(data);
					col.dataField = data;
					grid.columns.push(data);
				}
				
				//create a new ArrayCollection passing the de-serialized Array
				//ArrayCollections work better as DataProviders, as they can
				//be watched for changes.
				var dp:ArrayCollection = new ArrayCollection(arr);
				
				//pass the ArrayCollection to the DataGrid as its dataProvider.
				grid.dataProvider = dp;
			}
			
			private function onJSONFault(evt:FaultEvent):void {   
				trace(evt.message);   
			}        
		]]>
	</fx:Script>
	
	<mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10"/>
	
</s:WindowedApplication>

 

 

 

 

Developer
Posts: 425
Registered: ‎03-17-2011
My Device: Developer
My Carrier: Telus

Re: CANNOT PARSE JSON FILE URGENT...........

Please take a close look at the comment in your code:

 

//in this case, the JSON data is a serialize Array of Objects.
                var arr:Array = (JSON.decode(rawData) as Array);

 

Your code is designed to process an array of objects this is why it chokes when you pass a single object to it. 

_________________________
In the dark and need a sky map?
Discover What's up at App World.
Follow What's up on Facebook

Contributor
Posts: 40
Registered: ‎04-26-2011
My Device: blackberry playbook
My Carrier: Airtel

Re: CANNOT PARSE JSON FILE URGENT...........

how do i go about solving this problem??? ne suggestions???

Developer
Posts: 425
Registered: ‎03-17-2011
My Device: Developer
My Carrier: Telus

Re: CANNOT PARSE JSON FILE URGENT...........

Your need to write a parser specifically designed to parse your data. You cannot just lift an example form somewhere and expect it to work with unrelated data structures.

 

Try this for a start:

 

//your data is an object so you cannot cast it as an Array

var dataSmiley Surprisedbject  = (JSON.decode(rawData) as Object); 

// "name" is a property in the object.

 trace( data.name ); 

 

P.S.

Note that this is pretty basic stuff. There are lots of tutorials on the web - this is not the place to ask for help with this.

_________________________
In the dark and need a sky map?
Discover What's up at App World.
Follow What's up on Facebook

Contributor
Posts: 40
Registered: ‎04-26-2011
My Device: blackberry playbook
My Carrier: Airtel

Re: CANNOT PARSE JSON FILE URGENT...........

i am completely new to json and flex.it would be very helpful if you provide me with some tips.............