Variable name is an integer in ActionScript 3 - json

I am pulling .json info from weatherunderground into Flash CC. After I parse the data I am left with the following variables:
I am trying to get to the "conditions" variable. But I can't get there because of the integer/number "0" How do I format it?
This is my code:
function completeHandler(event: Event): void
{
var loader1: URLLoader = URLLoader(event.target);
var data1: Object = JSON.parse(loader1.data);
trace(data1.forecast.simpleforecast.forecastday.0.conditions);
}
This works for variables that don't have an integer in their path.

forecastday is an array, which you can see in the Value column ([] (#...). The JSON is just telling you what is contained in each element of the array, so just use data1.forecast.simpleforecast.forecastday[0].conditions, or loop through for each day.

Related

trying to convert data from Domino Access Service to a JSON string via JSON.stringify

I want to store the result from a call to a Domino Access Service (DAS) in a localStorage however when I try to convert the result object to a JSON string I get an error.
With DAS you get the result as an Array e.g.:
[
{
"#entryid":"1-CD90722966A36D758025725800726168",
"#noteid":"16B46",
Does anyone know how I get rid of the square brackets or convert the Array quickly to a JSON object?
Here is a snippet of my code:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
localStorage.setItem('myCatalog',JSON.stringify(data));
}
});
Brackets are part of the JSON syntax. They indicate that this is an array of objects. And as you point to a view it is very likely that you would get more than one object back (one for each entry in the view).
So if you are only interested in the first element you could do this:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
var firstRecord = data[0] || {};
localStorage.setItem('myCatalog',JSON.stringify(firstRecord));
}
});
Otherwise, you would need to define a loop to handle each of the objects :-)
/John

Flash: Parsing a JSON file that has a node called Class

Essentially I'm trying to parse JSON and assigning the results to variables, one of which is "var = JSON.class;" with class being what's returned in the JSON. However, flash won't let me parse it because it's called class which it uses to create new classes. Is there any workaround or will I not be able to grab this node?
I'm not sure if this is what you're asking, but if you have a property in your JSON object named "class", you should be able to parse it like as shown below.
// assuming JSON object looks like this, and is stored in a var named 'jsonData'
var jsonData:Object = { "id": 0, "class": "MyClassName", "values": [1,2,3] }
// trying to parse like this won't work w/keywords like 'class':
var parsedValue : String = jsonData.class;
// parse it with this way, using the square brackets:
var parsedValue : String = jsonData["class"];

Parsing url parameters in a String

I am using the example code in flash. I want a single variable and not the whole text.
I have a dynamic textfield called OUTPUT on the stage.
var fl_TextLoader:URLLoader = new URLLoader();
var fl_TextURLRequest:URLRequest = new URLRequest("http://www.testing.com/Christmas.txt");
fl_TextLoader.addEventListener(Event.COMPLETE, fl_CompleteHandler);
function fl_CompleteHandler(event:Event):void
{
var textData:String = new String(fl_TextLoader.data);
OUTPUT.text = textData;
}
fl_TextLoader.load(fl_TextURLRequest);
The Christmas text file contents:
Var1=Jesus&Var2=Mary&Var3=Christmas
The OUTPUT comes out with the whole string. How do I get the url parameter values separately?
Like OUTPUT.text = textData.Var1; (<--- But this does not work.)
The .data property is just a string, the raw data returned by the HTTP call, so you will have to parse the variable-value pairs, either using simple .split() on the strings or using the URLVariables object, that can do the parsing for you:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLVariables.html#decode()

Flex3 issue to get the array format using json object

{"object":[{"cyclename":"PE cycle","avgRunTime":"05:30","actualStartTime":"08/27/2011 02:40:08","actualEndTime":"08/27/2011 05:26:38","startTime":"02:40","status":"G"}]}
this is my file and i want to parse it to array and get the status displayed but i am getting data like [object object][object Object],[object Object],[object Object] etc...
how do i parse it to a dataprovider and code i have written is
private function cycle_resultHandler(event:ResultEvent):void
{
var myData:Object = JSON.decode(event.result as String);
for(var i:String in myData['object'])
{
dProvider.addItem(myData['object'][i]);
}
}
Your looping seems to be a bit off. First off you may want to reconsider your usage of a "for ...in" loop vs. a "for each" loop. This article: for...in vs. for each explains the differences quite plainly.
You may also want to give this article a read for more information on object introspection (a technique for determining the properties of a class at runtime -- what you are trying to do...).
In any case, the issue here is what you are looping over. If the goal here is to loop over the property values of "object" and add them to an array or arraycollection, you're almost there--
utilizing a "for each" loop you might do this instead:
private function cycle_resultHandler(event:ResultEvent):void {
var myData:Object = JSON.decode(event.result as String);
//Here we are iterating over the values in the "object" object as opposed to it's keys.
for each(var str:String in myData['object']) {
dProvider.addItem(str);
}
}
Hope this helps :)

AS3 I need to convert a string from input to an AS3 object

How can I do this? Here's what I have:
function send(input):void{
// input.text = "{key: 'value'}"
var x:* = stringToObject(input.text)
// then be able to do this
var y:* = x.key;
// then y must be equal to 'value'
trace(y) // this is just a string
}
You'll need to include the JSON library to be able to parse JSON strings into objects.
Reference the as3corelib library for more info.