Parsing a json value returned via rest api call

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Parsing a json value returned via rest api call

Post by jekyllandhyde » Wed Sep 21, 2016 7:31 pm

OK I'm pretty much a beginner but slogging along and making some progress.
When I make a rest api call such as this:

Code: Select all

put URL "http://????/signalk/v1/api/vessels/self/environment/depth" into depthvariable
It returns: {"belowKeel":{"value":10.630000}}
Json structure.

I'd like to parse out just the value. The following code doesn't seem to elegant. I'm sure there is a better way but I'd like to keep it simple?

Code: Select all

put char 23 to 31 of depthvariable into fld "depth"

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Parsing a json value returned via rest api call

Post by ClipArtGuy » Wed Sep 21, 2016 9:17 pm

Using the new JSONimort function you can take JSON and convert it into a LiveCode Array for easier handling. You can also use the JSONexport() function to turn LC arrays back into JSON.

Code: Select all

  put URL "http://????/signalk/v1/api/vessels/self/environment/depth" into depthvariable
   put jsonimport(depthvariable) into tArray
   put tArray[belowkeel]["value"] into depthvariable
hope that helps.

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Parsing a json value returned via rest api call

Post by jekyllandhyde » Wed Sep 21, 2016 10:00 pm

Thanks, that's awesome, much appreciated!

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Parsing a json value returned via rest api call

Post by jekyllandhyde » Fri Sep 23, 2016 6:35 pm

A "hopefully" quick easy followup question.

If I want to display the entire contents of the array in a table format I try this:

Code: Select all

put URL "http://????/signalk/v1/api/vessels/self/environment/depth" into depthvariable
put jsonimport(depthvariable) into tArray
combine tArray by return and tab
--"arraydata" is a table field
put tArray into field "arraydata"
As per the previous answer, in row 1 column 1 it shows "belowkeel" but there is no associated value in row 1 column 2?
Somewhere the value gets dropped?

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Parsing a json value returned via rest api call

Post by ClipArtGuy » Fri Sep 23, 2016 11:22 pm

I am not too familiar with how combine works, but I can confirm that the value is dropped on my end as well.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9837
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Parsing a json value returned via rest api call

Post by FourthWorld » Sat Sep 24, 2016 1:35 am

Check the keys of the array. Ofhand it looks like the source data has an array as the value of outer array element. So instead of this:

Code: Select all

combine tArray by return and tab
put tArray into field "arraydata"
...you may need to work on the contents of the inner array:

Code: Select all

put tArray["belowKeel"] into tSubArray
combine tSubArray by return and tab
put tSubArray into field "arraydata"
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Parsing a json value returned via rest api call

Post by jekyllandhyde » Mon Sep 26, 2016 1:31 am

Thanks, much appreciated.

That worked great, I ended up using this:

Code: Select all

on mouseUp
local tArray
put URL "http://999999999/signalk/v1/api/vessels/self/environment/depth" into depthvariable
put jsonimport(depthvariable) into tArray
put tArray["belowKeel"] into tSubArray
combine tSubArray by return and tab
combine tArray by return and tab
put tArray into field "arraydata"
put tSubArray after field "arraydata"
This put all three values into three cells in one row:

belowKeel, value, 23.600000

the raw json data looks like:

{"belowKeel":{"value":23.600000}}

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Parsing a json value returned via rest api call

Post by jekyllandhyde » Tue Sep 27, 2016 8:37 am

two steps forward, one step back...

I need some advice as to the best approach for this design:

Imagine a card with 4 data fields constantly updating. As soon as the card is opened the four data fields start making REST API calls to get values to put into the fields. I need to have them all simultaneously updating. No user intervention needed or wanted. Field update automatically every 2 seconds or so.

After a hunt through all the documentation I'm at a loss as to the best approach for this. I was going to send a mouse up command to a hidden button containing each script:

card script

Code: Select all

on opencard
send mouseup to fld "1" 
send mouseup to fld "2"
send mouseup to fld "3"
send mouseup to fld "4"
end opencard
In each button script I have a repeat forever loop which does everything, waits a couple of seconds, and then updates the field.

I'm sure this is completely the wrong way to do this because I'm sure the repeat loops interfere with each other...

Please start me on a better approach!

peter-b
Posts: 182
Joined: Thu Nov 20, 2014 2:14 pm
Location: LiveCode Ltd.

Re: Parsing a json value returned via rest api call

Post by peter-b » Mon Oct 03, 2016 10:30 am

Consider something like this (I haven't compiled, run or tested this, so use at your own risk):

Code: Select all

on startPolling
   set the cPollEnabled of me to true
   send "doPoll" to me in 0
end startPolling

on stopPolling
  set the cPollEnabled of me to false
end stopPolling

on doPoll
   if the cPollEnabled of me is not true
      exit doPoll
   end if

   -- Do your polling stuff here

   send "doPoll" to me in 2 seconds
end doPoll
LiveCode Open Source Team — @PeterTBBrett — peter.brett@livecode.com

jekyllandhyde
Posts: 92
Joined: Thu Feb 14, 2013 7:17 pm

Re: Parsing a json value returned via rest api call

Post by jekyllandhyde » Tue Oct 11, 2016 7:49 pm

Peter,

Thanks for the suggestion, I'm working on that approach, seems promising.

Cheers, Adam

Post Reply

Return to “iOS Deployment”