Page 1 of 1
Parsing a json value returned via rest api call
Posted: Wed Sep 21, 2016 7:31 pm
by jekyllandhyde
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"
Re: Parsing a json value returned via rest api call
Posted: Wed Sep 21, 2016 9:17 pm
by ClipArtGuy
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.
Re: Parsing a json value returned via rest api call
Posted: Wed Sep 21, 2016 10:00 pm
by jekyllandhyde
Thanks, that's awesome, much appreciated!
Re: Parsing a json value returned via rest api call
Posted: Fri Sep 23, 2016 6:35 pm
by jekyllandhyde
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?
Re: Parsing a json value returned via rest api call
Posted: Fri Sep 23, 2016 11:22 pm
by ClipArtGuy
I am not too familiar with how combine works, but I can confirm that the value is dropped on my end as well.
Re: Parsing a json value returned via rest api call
Posted: Sat Sep 24, 2016 1:35 am
by FourthWorld
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"
Re: Parsing a json value returned via rest api call
Posted: Mon Sep 26, 2016 1:31 am
by jekyllandhyde
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}}
Re: Parsing a json value returned via rest api call
Posted: Tue Sep 27, 2016 8:37 am
by jekyllandhyde
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!
Re: Parsing a json value returned via rest api call
Posted: Mon Oct 03, 2016 10:30 am
by peter-b
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
Re: Parsing a json value returned via rest api call
Posted: Tue Oct 11, 2016 7:49 pm
by jekyllandhyde
Peter,
Thanks for the suggestion, I'm working on that approach, seems promising.
Cheers, Adam