Getting JSON data from a website using Delphi [clo

2019-02-11 12:54发布

问题:

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.

回答1:

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...):

  • http://jvcl.svn.sourceforge.net/viewvc/jvcl/trunk/jvcl/examples/JvProgramVersionCheck/
  • http://jvcl.svn.sourceforge.net/viewvc/jvcl/trunk/jvcl/examples/JvUrlListGrabber/

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:

  • http://blog.synopse.info/post/2012/05/03/Custom-JSON-serialization-of-records
  • http://blog.synopse.info/post/2012/04/13/Custom-JSON-serialization-of-any-class
  • http://blog.synopse.info/post/2012/04/12/Custom-JSON-serialization-of-any-dynamic-array-content

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



回答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!