Current News

Moderator: Mmiscool

User avatar
By cicciocb
#42199 Hi all,
some little explanations about the json parser.
First of all, it's not a real parser but more a "finder" instructed to follow JSON rules !
It find, in the order, all the terms specified separated by the "." (point).

Let's see with an example with OpenWeatherAPI :
the query is :
let appid = "YOUR APPID"
let query = "http://api.openweathermap.org/data/2.5/weather?&units=metric&q=Miami,us&appid=" & appid
let ret = wget(query)
serialprintln ret
---------------------------------------------------------------
{"coord":{"lon":-80.19,"lat":25.77},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"cmc stations","main":{"temp":23.38,"pressure":1031.39,"humidity":65,"temp_min":23.38,"temp_max":23.38,"sea_level":1031.65,"grnd_level":1031.39},"wind":{"speed":2.97,"deg":123.001},"clouds":{"all":36},"dt":1456871476,"sys":{"message":0.0101,"country":"US","sunrise":1456832554,"sunset":1456874597},"id":4164138,"name":"Miami","cod":200}

Now you can find any element as follow :
let desc = json(ret,"weather.description")
let temp = json(ret,"main.temp")
let press = json(ret,"main.pressure")
let humid = json(ret,"main.humidity")

let tim = json(ret,"dt") ' this is the time
let tim = unixtime(tim) ' this function converts the time from the unix format to "standard format";
let name = json(ret,"sys.name")

Now there is another function dedicated to OpenWeatherApi but more for the forecast.
As the forecast messages can be very big and cause the saturation of the ESP memory (with the associated crash), there is another function that permit to "filter" the message.

For example, this query generate a very long message (around 15 Kb):

let query = "http://api.openweathermap.org/data/2.5/forecast?&units=metric&q=Miami,us&appid=" & appid

So, there is another function that able to choose one item from the list; this is extracted from the stream with a minimum use of the memory.

Example :

let query = "http://api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=Miami,us&appid=" & appid
let ret = readopenweather(query,1) ' the 1 means the first item of the list - 0 means the root
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
serialprint tim

If you need another item from the list, simply repeat the request using another index.

Regards,
CiccioCB
User avatar
By forlotto
#42217 I am a bit lost on this example but I'd have to take time to study it.

I am more curious on how to use arrays possibly stored on the device or how to grab user input data to store it in an array possibly for things like a timer for instance.

I think json could be very very useful and I thank you for this nice fork! Speed improvements and json now we can even skin the editor I hear all hail black with green font heh.

Anyhow can't wait to see what people come up with now that json is available for me I'd have to learn a bit more through examples from the community or just simply read up on json a bit more I know its way popular but when you do basic webpages and just odds and ends stuff it will be reading required I'm sure.