0

There is this website http://www.ingress.com/intel

To access the website, we must login using username and password.

Once accessed, the site uses JSON for its data.

I am new to this JSON thing.

Anyone can give a general example how to get JSON data from a website using Delphi?

I am using Delphi 7 by the way.

Thanks.

Arioch 'The
  • 15,799
  • 35
  • 62
Kawaii-Hachii
  • 1,017
  • 7
  • 22
  • 36
  • 1
    TIdHTTP.Get is what you're looking for, regarding JSON, think of it as a "text file", you need to figure out how to do the "login"(what location you need to hit and what are the param names for user name and password), enable "keep cookies" in TIdHTTP, finally, just hit the links that will return a JSON "file". –  Apr 11 '13 at 11:53
  • possible duplicate of http://stackoverflow.com/q/10323145/33732 – Rob Kennedy Apr 11 '13 at 12:19
  • 3
    @Arioch, based on my quick read of the code, Superobject does not require generics. It will use generics and new RTTI if they're available. [Revision 51](https://code.google.com/p/superobject/source/detail?r=51) was made specifically for Delphi 7 compatibility. – Rob Kennedy Apr 11 '13 at 12:50
  • 1
    I taught myself how JSON works with all the beautiful resources online, which can be found with Google. – Jerry Dodge Apr 11 '13 at 14:08
  • 1
    @ComputerSaysNo Thanks for your simple explanation. That's what I am looking for. So, basically, I can simply use HTTP GET or POST, then the JSON data will be there in the "html source" that is received. Ok, I understand now :) – Kawaii-Hachii Apr 12 '13 at 07:35
  • @ewlung pretty much, of course you have to parse the JSON to retrieve data and push it to db or whatever, but in a nutshell that's it... for JSON parsing, I recommend dwsJSON.pas(requires a few *.inc and *.pas) from DWScript project http://code.google.com/p/dwscript/source/browse/#svn%2Ftrunk%2FSource –  Apr 12 '13 at 13:35

2 Answers2

9

Those are two questions.

  1. how to get file from HTTP server
  2. how to parse JSON string

For 1st question there are a lot of libraries, some of those:

  1. Internet Direct aka Indy Sockets (distributed with Delphi and http://www.indyproject.org/)
  2. Internet Components Suite aka ICS http://www.overbyte.be
  3. ararat Synapse http://synapse.ararat.cz/
  4. UrlListGrabber from JEDI VCL http://jvcl.sf.net
  5. Windows InternetReadFile http://msdn.microsoft.com/en-us/library/windows/desktop/aa385103.aspx
  6. ...and many many more.

For example this is a sample of getting file from internet: http://synapse.ararat.cz/doku.php/public:howto:httpgetpage

Or another lib's demos (actually every lib has them, loading file from HTTP is so common task...):


As for JSON parsers, after you downloaded content from net, that is a narrower choice: JSON is a relatively recent trend and many JSON parsers heavily use features of Delphi 2009+
Personally i think you'd better try to use JSON parser from mORMot project. That project also has HTTP layer so perhaps you can use their code to read file over HTTP as well.

There are examples or parsing JSON string:

More info at http://synopse.info/ and http://blog.synopse.info/tag/JSON

They also have forum with responsive community.

SuperObject, while originally built around generics (practically working since Delphi 2010), also has Delphi 7 compatibility, though some of its features would not work. See http://code.google.com/p/superobject/wiki/first_steps

And you always can search for more libraries on http://www.torry.net

Arioch 'The
  • 15,799
  • 35
  • 62
2

I suspect it will be pretty difficult to log in this specific website from a Delphi application.

You will need to log via a Google account, and handle cookies as expected.

Therefore, I'm afraid you won't be able to use any direct Indy / Synapse / ICS / WinINet library to access the data.

You will probably need to embed a full browser within the application, then access to the content from the JavaScript point of view, remoting the browser from your application.

Take a look at those components:

  • TWebBrowser (i.e. Internet Explorer) as available in Delphi;
  • Delphi Chromium.

Then you will have to parse the JSON content. But you must first ensure that you can login the application!

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159