JSON Parsing

158 views
Skip to first unread message

Ronald Muller

unread,
Sep 17, 2013, 4:05:50 PM9/17/13
to comman...@googlegroups.com

iPad 1, IOS 5.1.1, iViewer 4.0.263, Guidesigner 2.6.0.602

I'm trying to parse a file that I download from the internet using Javascript. The file contains weather data in JSON format. my goal is to parse this data into a JSON object in order to retrieve the weather date. The link to the data is:


The file I use can be found in the link: https://gist.github.com/ronnie72/6599746

When activation the getData function, the debugger displays:
20:29:08> SCRIPT: Status is: 200
20:29:08> SCRIPT: weather is: [object Object]
20:29:08> s7001 = { "data": { "current_condition": [ {"cloudcover": "50", "humidity": "87", "observation_time": "06:25 PM", "precipMM": "1.2", "pressure": "998", "temp_C": "10", "temp_F": "50", "visibility": "10", "weatherCode": "176",  "weatherDesc": [ {"value": "Patchy rain nearby" } ],  "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0025_light_rain_showers_night.png" } ], "winddir16Point": "SSW", "winddirDegree": "200", "windspeedKmph": "24", "windspeedMiles": "15" } ],  "request": [ {"query": "Vleuten, Netherlands", "type": "City" } ],  "weather": [ {"date": "2013-09-17", "precipMM": "9.3", "tempMaxC": "14", "tempMaxF": "57", "tempMinC": "10", "tempMinF": "50", "weatherCode": "353",  "weatherDesc": [ {"value": "Light rain shower" } ],  "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "SW", "winddirDegree": "235", "winddirection": "SW", "windspeedKmph": "24", "windspeedMiles": "15" }, {"date": "2013-09-18", "precipMM": "2.1", "tempMaxC": "15", "tempMaxF": "59", "tempMinC": "10", "tempMinF": "50", "weatherCode": "353",  "weatherDesc": [ {"value": "Light rain shower" } ],  "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "WNW", "winddirDegree": "286", "winddirection": "WNW", "windspeedKmph": "19", "windspeedMiles": "12" }, {"date": "2013-09-19", "precipMM": "1.7", "tempMaxC": "15", "tempMaxF": "60", "tempMinC": "11", "tempMinF": "52", "weatherCode": "353",  "weatherDesc": [ {"value": "Light rain shower" } ],  "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "WSW", "winddirDegree": "255", "winddirection": "WSW", "windspeedKmph": "18", "windspeedMiles": "11" }, {"date": "2013-09-20", "precipMM": "2.4", "tempMaxC": "16", "tempMaxF": "60", "tempMinC": "10", "tempMinF": "50", "weatherCode": "176",  "weatherDesc": [ {"value": "Patchy rain nearby" } ],  "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png" } ], "winddir16Point": "NW", "winddirDegree": "318", "winddirection": "NW", "windspeedKmph": "16", "windspeedMiles": "10" }, {"date": "2013-09-21", "precipMM": "0.0", "tempMaxC": "16", "tempMaxF": "61", "tempMinC": "12", "tempMinF": "53", "weatherCode": "119",  "weatherDesc": [ {"value": "Cloudy" } ],  "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0003_white_cloud.png" } ], "winddir16Point": "WSW", "winddirDegree": "251", "winddirection": "WSW", "windspeedKmph": "6", "windspeedMiles": "4" } ] }}

The DisplayData functions however display this in the debugger:

20:29:11> SCRIPT: Exception running code received from iViewer: ReferenceError: Can't find variable: weather
20:29:11> SCRIPT: Code that caused the exception: (function(){var join="d0",tokens={"[join]":"d0"},list=null,listIndex=0;WTH.DisplayData()})();


Can somebody help me out here and help me understand what is happening?

Barry Gordon

unread,
Sep 17, 2013, 4:31:15 PM9/17/13
to comman...@googlegroups.com

JSON files come in as a string. You need to take that string and act on it with the as in

 

var weather=JSON.parse(stringofJSONWeather);

 

You might want to take a look at the setup.js module I sent you a while back, look at the internal function loadOneFile.

--
You received this message because you are subscribed to the Google Groups "CommandFusion Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to commandfusio...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ronald Muller

unread,
Sep 17, 2013, 4:36:58 PM9/17/13
to comman...@googlegroups.com
Thanks Barry, I went though your files before but could not find what I was looking for. Will try again, now I know what to look for.

Jarrod Bell

unread,
Sep 17, 2013, 7:27:35 PM9/17/13
to comman...@googlegroups.com
We need to see the code that is actually causing the error. Sounds like you are accessing a variable, weather, where it is out of scope.

Regards,

Jarrod Bell
CommandFusion
www.commandfusion.com


Ronald Muller

unread,
Sep 18, 2013, 1:09:57 AM9/18/13
to comman...@googlegroups.com, jar...@commandfusion.com
Hi Jarrod,

The code is in the Gist link. https://gist.github.com/ronnie72/6599746. The functions GetData and DisplayData are called by a button press in the GUI.

Jarrod Bell

unread,
Sep 18, 2013, 1:28:44 AM9/18/13
to comman...@googlegroups.com
Line 21 you create a local variable called 'weather' that is only accessible within the 'IF' statement enclosing it.
You cannot reference this variable anywhere else in your code, especially not in another function such as on line 36.

Also, line 23 you are appending an object 'weather' to a string to log, you should instead use CF.logObject(weather); as the variable weather is not a string, it's an object.

So you need to move your weather variable declaration outside the function definitions, most likely within your WTH object.

I have forked your gist and made some (untested) changes here: https://gist.github.com/jarrodbell/6604897

Regards,

Jarrod Bell
CommandFusion
www.commandfusion.com


Ronald Muller

unread,
Sep 18, 2013, 3:11:41 PM9/18/13
to comman...@googlegroups.com
Thanks, got it to work now.

Barry Gordon

unread,
Sep 18, 2013, 4:53:29 PM9/18/13
to comman...@googlegroups.com
I just made a change to my projects architecture. Instead of keeping the
configuration files (JSON format) as text files in an Asset folder, I know
have them all in a single JS module named configuration.js with each
configuration file being an object of the master object CFG. The module
looks something Like:
var CFG={
//
control:
{ // The control object holds the names of each other sub object e.g.
configuration_Object_1, configuration_Object_2, . . .
},
//
interface_object:
{ // the interface object holds information about what is connected to
what; IP adresses, IR mapping, etc
},
//
configuration_Object_1:
{ // There are three general types of configuration objects:
// One type completely describe all the devices in my home how they are
graphically displayed so a single block of code displays just about anything
// The second type holds channel and radio station lists
// The third type holds IR timing sequences in iTach format to control IR
based things (TV's Set top boxes, etc). A single JS module can send
},
//
.
.
.
//
last_Configuration_Object:
{
}
//
}; // end of the JSON object CFG which holds all the config objects
CF.modules.push({name:"Configuration", object:CFG});

This change now allows for zipped and encrypted outputs from guiDesigner to
put out all of the "Asset" resources even though they are not mentioned in
the XML output of guiDesigner.




-----Original Message-----
From: comman...@googlegroups.com [mailto:comman...@googlegroups.com]
On Behalf Of Ronald Muller

Ronald Muller

unread,
Sep 23, 2013, 2:47:06 PM9/23/13
to comman...@googlegroups.com
I have been starring at this for days now and do not understand why some items in the following function can not be retrieved.

Weatherforecastlist: function() {
       var qItem ={};
        var weatherforecastarray = [];
        for (var i = 0; i < 12; i=i+2) {
            qItem={
                s7100: WTH.weatherforecast.forecast.txt_forecast.forecastday[i].title,
                s7101: WTH.weatherforecast.forecast.txt_forecast.forecastday[i].icon_url,
                s7102: WTH.weatherforecast.forecast.simpleforecast.forecastday[i].low.celsius + " °C",
                s7103: WTH.weatherforecast.forecast.simpleforecast.forecastday[i].high.celsius,
                s7104: WTH.weatherforecast.forecast.simpleforecast.forecastday[i].avewind.dir,
                s7105: WTH.weatherforecast.forecast.simpleforecast.forecastday[i].avewind.kph + " kph",
                s7106: WTH.weatherforecast.forecast.simpleforecast.forecastday[i].avehumidity + " %",
                s7107: WTH.weatherforecast.forecast.simpleforecast.forecastday[i].qpf_day.mm + " mm",
                s7108: WTH.weatherforecast.forecast.txt_forecast.forecastday[i].fcttext_metric,
                s7109: WTH.weatherforecast.forecast.simpleforecast.forecastday[i].conditions
            };
         weatherforecastarray.push(qItem);
        };
       CF.listRemove("l71");
       CF.listAdd("l71", weatherforecastarray);
   },

When I only try to load the items "txt_forecast" the information loads fine in the list. But the "simpleforecast" items generate an error:

SCRIPT: Exception: TypeError: 'undefined' is not an object (evaluating 'WTH.weatherforecast.forecast.simpleforecast.forecastday[i].low')

When display the object with CF.logObject (Thanks Jarrod..), I see the following, not sure this has something to do with it (just one line out of the log).

........low = object (increase log depth to see contents)

I have attached the JSON file I'm parsing.

  
JSON.rtf

Clayton Vicente

unread,
Sep 23, 2013, 3:18:46 PM9/23/13
to comman...@googlegroups.com
Just to understand, why are you using a loop if there is only one item inside the WTH.weatherforecast.forecast.simpleforecast ?
Or you dont send the full JSON ?
Clayton


Date: Mon, 23 Sep 2013 11:47:06 -0700
From: ronald...@me.com

To: comman...@googlegroups.com
Subject: Re: JSON Parsing

Clayton Vicente

unread,
Sep 23, 2013, 3:34:56 PM9/23/13
to comman...@googlegroups.com
Ops, my bad Ronald, i see now, i was looking in the iPad.

From: clayt...@hotmail.com
To: comman...@googlegroups.com
Subject: RE: JSON Parsing
Date: Mon, 23 Sep 2013 20:18:46 +0100

Clayton Vicente

unread,
Sep 23, 2013, 3:47:26 PM9/23/13
to comman...@googlegroups.com
Ok, diferent from the s7100: WTH.weatherforecast.forecast.txt_forecast.forecastday[i] that have 20 items, the WTH.weatherforecast.forecast.simpleforecast.forecastday[i] has only 10, so the max value of i for this item will be 9, your loop count until 10, this will give you a error.
At least i think so.
Clayton



From: clayt...@hotmail.com
To: comman...@googlegroups.com
Subject: RE: JSON Parsing
Date: Mon, 23 Sep 2013 20:34:56 +0100

Ronald Muller

unread,
Sep 23, 2013, 4:02:08 PM9/23/13
to comman...@googlegroups.com

Clayton, you are correct. Just a quick test with the loop at max 10 returns the results. I'll have to clean this up and use two loops to get this info I need.

Thank you very much for your help.

Clayton Vicente

unread,
Sep 23, 2013, 4:15:51 PM9/23/13
to comman...@googlegroups.com
Good to know.
Just a tip. When we have a array inside a object there is a way to prevent this error. Get size of the array

var  simpleforecastLength =   WTH.weatherforecast.forecast.txt_forecast.forecastday.length
var  forecasttxtLength =   WTH.weatherforecast.forecast.simpleforecast.forecastday.length
Now just use in the loop

for(i=0;i<simpleforecastLength;i++){
   //insert your code here
}

for(j=0;j<forecasttxtLength ;j++){
   //insert your code here
}
Clayton


Date: Mon, 23 Sep 2013 13:02:08 -0700

From: ronald...@me.com
To: comman...@googlegroups.com
Subject: Re: JSON Parsing


Clayton, you are correct. Just a quick test with the loop at max 10 returns the results. I'll have to clean this up and use two loops to get this info I need.

Thank you very much for your help.

Ronald Muller

unread,
Sep 23, 2013, 4:38:38 PM9/23/13
to comman...@googlegroups.com
Thanks! I had it build using .length property but could not get it to work and started using integers instead to test. I Will build the two loops to get the information I need.
Reply all
Reply to author
Forward
0 new messages