Trouble parsing Json

Hi all,

I don't get why what I implement doesn't work.
Any idea?

I would like to retreive "lat" and "lon" from this Json.

This is what the JSON looks like : 

{
"associated":{"utcOffset":2,
		"units":{"temperature":"C","tideHeight":"M","swellHeight":"M","waveHeight":"M","windSpeed":"KPH"},
		"tideLocation":{"name":"Les Sables d'Olonne","min":-0.16,"max":6.09,"lon":-1.7833,"lat":46.5,"mean":0}
	       },

...

}

And that's what I implemented : 

	 // INIT LAT AND LON 
		 	var associated = data["associated"];
		 	var tideLocation = associated["tideLocation"];
		 	var Lat = tideLocation["lat"];
	    	var Lon = tideLocation["lon"];

Where "data" is the JSON pushed as a parameter of the onReceive function.

Any advice?

  • Not sure why you are using data[35], when it should be data["whatever"] as it's a dictionary.  The data might not always be in the same order, and data[35] could be different things.

    When in doubt, add println calls, and you can include things like System.getDeviceStats().usedMemory and totalMemory and freeMemory.

  • Hi Jim, thanks for your reply.

    I pass an array to background.exit(), and retreive this array under the name of "data" in the data processing function.

    Then I use the data array elements to process and store using Applciation.Storage

    data[35] is the latest value I inserted in the array in the background.

  • So if I'm understanding right:

    - Your background process is making a web request

    - The web request returns JSON which is automatically turned into a dictionary

    - In your background process, you parse the dictionary and return an array in Background.exit()

    - In your *app's* (foreground) data processing function, when you add code to parse the array returned by the background, it causes the background process's makeWebRequest to fail with -403?

    If that's the case it sounds like your data processing function (which reads the array) is a member of the App class (which belongs to both the background and foreground process). So whenever you add code to it, it consumes memory in both the foreground and background process (for the code itself).

    But since you only need to call the array-reading data processing function in the foreground, you should make the data processing function a member of your App's view (or a global function, or anything that isn't part of the App class itself.)

  • Hey Morning FlowState,

    Well, you got it all!

    Since my last post I managed to review my background.mc code to make it leaner. I now can parse everything I need and return that using an array. However I still get this -403 response when I try to parse this array in the foreground which supports your assumptions.

    At the moment my parsing function is held in class MyWatchFaceApp extends Application.AppBase.

    I'll take it out of this class, but will keep it the App.mc file making it global. Does that sounds good?

    Cheers,

    Nico

  • Your AppBase is loaded when you run the background or main app.  Some things are called in both (initialize, onStart,etc), some only by the background (getServiceDelegate. etc). and some only by the main app (onBackgroundData, etc).  Globals are available to both, but they are not the same.  if you have a global like

    var myData;

    both the background and main app can use myData, but it's not common between the two, so if you change myData in the background, the main app's version of myData doesn't change.