* * *

Author Topic: Retrieving json data from web source and parsing it  (Read 667 times)

MikeJazz

  • Newbie
  • Posts: 1
Retrieving json data from web source and parsing it
« on: February 28, 2017, 08:21:24 pm »
Hi guys,
so I'm fairly new coding in general and I'm look for some help to get me going with getting weather data from http://api.wunderground.com/api/Your_Key/features/settings/q/query.format.

Now I've filled in the part after .../api/... to reflect what i want to retrieve. Ang just typing it i to a browser i get what I'm looking for.
Now the part I need help is sending of the url, and then parsing the .json into variables that i can then use for my nefarious deeds.

I have added Synapse to my Lazarus and been playing with it, but to no avail-

- Mike

Phil

  • Hero Member
  • *****
  • Posts: 2041
Re: Retrieving json data from web source and parsing it
« Reply #1 on: February 28, 2017, 09:22:54 pm »
Hi guys,
so I'm fairly new coding in general and I'm look for some help to get me going with getting weather data from http://api.wunderground.com/api/Your_Key/features/settings/q/query.format.

Now I've filled in the part after .../api/... to reflect what i want to retrieve. Ang just typing it i to a browser i get what I'm looking for.
Now the part I need help is sending of the url, and then parsing the .json into variables that i can then use for my nefarious deeds.

I assume you substituted your own key for "Your_Key", right?

With Synapse, you would use the THTTPSend class. Do a GET request with the URL you want to send. If that succeeds, you can then read back the JSON returned. Use FPC's JSON units to parse the JSON returned.

Look for examples for how to use Synapse and the JSON units.

On a related note, in case you're interested, the ndfd library used with the example apps of Part 3 of the Dynamic Library article series gets weather forecast data from NDFD. It's somewhat different from WU, since it uses the NDFD SOAP service and thus the Web Service Toolkit to make the request. And the data returned is XML, not JSON. But it's there if you have a need for forecast data, in addition to WU historical weather data.

https://macpgmr.github.io/

SkyKhan

  • Full Member
  • ***
  • Posts: 155
Re: Retrieving json data from web source and parsing it
« Reply #2 on: February 28, 2017, 09:48:49 pm »
Code: Pascal  [Select]
  1. uses   fphttpclient, fpjson, jsonparser;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   Http: TFPHttpClient;
  6.   Content : string;
  7.   Json : TJSONData;
  8.  
  9. begin
  10.   Http:=TFPHttpClient.Create(Nil);
  11.   try
  12.      Content:=Http.Get('https://jsonplaceholder.typicode.com/users'); //  this is just an example data
  13.      JSON:=GetJSON(Content);
  14.      try
  15.         // You may look http://wiki.freepascal.org/fcl-json to see what you can do with TJSONData
  16.      finally
  17.        JSON.Free;
  18.      end;
  19.   finally
  20.     Http.Free;
  21.   end;
  22. end;
  23.  
I'm not here to learn but to help beginners mostly. So you dont really need to poke into my answers if you'll try to be pedantic. Really. If you really need to teach something , you can speak to questioners, not me. Thanks in advance.

BeniBela

  • Hero Member
  • *****
  • Posts: 547
    • homepage
Re: Retrieving json data from web source and parsing it
« Reply #3 on: February 28, 2017, 11:43:23 pm »
My internet tools put it all together. E.g.:

Code: [Select]
  uses xquery, xquery_json, synapseinternetaccess;
  query('json("http://api.wunderground.com/api/Your_Key/features/settings/q/query.format").forecast.txt_forecast.forecastday(1).fcttext').toString

Or step by step:

Code: [Select]
var
  forecasts: IXQValue;
begin
  forecasts := xqvalue('http://api.wunderground.com/api/Your_Key/features/settings/q/query.format').retrieve().map('(.).forecast.txt_forecast.forecastday()');
  forecasts.get(1).getProperty('fcttext').toString;
  forecasts.get(2).getProperty('fcttext').toString;
  forecasts.get(3).getProperty('fcttext').toString;
  ...

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus