Page 1 of 1

LUA json API what is wrong ? Help.

Posted: Sunday 14 May 2017 19:53
by janojj
Hello
I'm just starting with LUA I have a problem with the API and script.
Address to API http://powietrze.malopolska.pl/_powietr ... a&ci_id=23

My script that will not process data

Code: Select all

       json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux

       local config=assert(io.popen('curl http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=23'))
       local location = config:read('*all')
       config:close()
       local jsonLocation = json:decode(location)

    d1 = jsonLocation.dane.actual.details.0.par_desc
d2 = jsonLocation.dane.actual.details.0.g_nazwa
d3 = jsonLocation.dane.actual.details.1.par_desc
d4 = jsonLocation.dane.actual.details.1.g_nazwa
d5 = jsonLocation.dane.actual.details.2.par_desc
d6 = jsonLocation.dane.actual.details.2.g_nazwa
   print('Lat: '..d1)

Wants to download data

d1 = jsonLocation.dane.actual.details.0.par_desc
d2 = jsonLocation.dane.actual.details.0.g_nazwa
d3 = jsonLocation.dane.actual.details.1.par_desc
d4 = jsonLocation.dane.actual.details.1.g_nazwa
d5 = jsonLocation.dane.actual.details.2.par_desc
d6 = jsonLocation.dane.actual.details.2.g_nazwa

I have such a mistake
Malformed number near '.0.par_desc'

When he wants to download other data
d1 = jsonLocation.dane.city.ci_name
lua: test.lua:9: attempt to index field 'dane' (a nil value)
stack traceback:
test.lua:9: in main chunk
[C]: ?

Please help the new one.

Re: LUA json API what is wrong ? Help.

Posted: Tuesday 13 June 2017 13:04
by zicht
--

Re: LUA json API what is wrong ? Help.

Posted: Tuesday 13 June 2017 13:07
by zicht
I have no clue of what you try to do at all..

Re: LUA json API what is wrong ? Help.

Posted: Tuesday 13 June 2017 16:09
by jvdz
Shouldn't that be:

Code: Select all

d1 = jsonLocation.dane.actual.details[0].par_desc
etc..
Jos

Re: LUA json API what is wrong ? Help.

Posted: Thursday 15 June 2017 20:19
by janojj
Hello so this is my start in lua. Try

d1 = jsonLocation.dane.forecast.dzisiaj.details[0].fo_nazwa

Unfortunately it does not work

lua: 1.lua:8: attempt to index field 'dane' (a nil value)
stack traceback:
1.lua:8: in main chunk
[C]: ?

what is wrong?

please help.

Re: LUA json API what is wrong ? Help.

Posted: Thursday 15 June 2017 21:15
by jvdz
Actually several things are wrong. A simple debug statement revealed that the curl returned: {"exception":"Nieprawid\u0142owy parametr"}

So I did some testing and made the required modifications to your script:

Code: Select all

-- json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
local json = require "JSON" -- for raspberry
local config = assert(io.popen('curl "http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=23"'))
local location = config:read('*all')
config:close()
--~ print(location)
local jsonLocation = json:decode(location)
d1 = jsonLocation.dane.actual[1].details[1].par_desc
d2 = jsonLocation.dane.actual[1].details[1].g_nazwa
d3 = jsonLocation.dane.actual[1].details[2].par_desc
d4 = jsonLocation.dane.actual[1].details[2].g_nazwa
d5 = jsonLocation.dane.actual[1].details[3].par_desc
d6 = jsonLocation.dane.actual[1].details[3].g_nazwa
print('Lat: '..d1)
Jos

Re: LUA json API what is wrong ? Help.

Posted: Friday 16 June 2017 8:30
by janojj
Hello
I tested

Code: Select all

       json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux

       local config=assert(io.popen('curl http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=21'))
       local location = config:read('*all')
       config:close()
       local jsonLocation = json:decode(location)

d1 = jsonLocation.dane.forecast.dzisiaj[0].details[0].par_name
   print('Lat:'..d1)
it still does not work. What am I doing wrong
Thanks in advance for your help

Re: LUA json API what is wrong ? Help.

Posted: Friday 16 June 2017 9:08
by jvdz
Did the code I gave work for you?
Why do you use [0] in stead of [1] like I did? The decoded array starts with [1] I think.
EDIT: I see you use a different url and guess you need this key:
d1 = jsonLocation.dane.forecast.dzisiaj.details[1].par_name

Jos

Re: LUA json API what is wrong ? Help.

Posted: Saturday 17 June 2017 9:53
by janojj
Thank you very much for help. The script works great. :lol: