Recent

Author Topic: FPC\Lazarus Library for JSON  (Read 23308 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
FPC\Lazarus Library for JSON
« on: September 25, 2012, 02:12:53 pm »
Hi

I have manually coded some functions for reading and parsing some JSON (http://www.json.org/) structures. But I've 'done it the hard way', so to speak using various Pos values and reading string values inbetween. I wondered if there is a maintained library of sorts that would no doubt do it better?

I found http://code.google.com/p/json-fpk/ and http://code.google.com/p/delphijson/ and also mention of TJSONObject but I am struggling to find the FPC documentation for TJSONObject and I didn't want to go ahead with the first two if there's a built maintained way of doing it with Lazarus\FPC without relying on third party modules?

I also found this Delphi library : http://stackoverflow.com/questions/4350886/delphi-parse-json-string - not sure if there is the same for freepascal?

Can anyone give me some pointers for looking for and then reading JSON structures using Lazarus and FPC?

Thanks

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: FPC\Lazarus Library for JSON
« Reply #1 on: September 25, 2012, 02:22:50 pm »
See fpctwit [1] for an example of using the built in FPC JSON units.

Note:
- Support for UTF8 encoding was only added recently (I think to FPC trunk, probably not to 2.6.1 definitely not to 2.6.0)
- fpctwit provides a UTF8 version that can be used for older fpcs; once again see the code to see how FPC version detection is done for that.
- see e.g. ProcessTweets in twitter.pas in that repository for examples on decoding json. the plurk example client code has a lot of json, too

[1] https://bitbucket.org/reiniero/fpctwit/src
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPC\Lazarus Library for JSON
« Reply #2 on: September 25, 2012, 06:47:20 pm »
Quote
but I am struggling to find the FPC documentation for TJSONObject
When a package has no documentation, it's the source code and that serves it (well you have them all open, feel free to look at it, there are some examples which you can learn from).

loopbreaker

  • New Member
  • *
  • Posts: 32
Re: FPC\Lazarus Library for JSON
« Reply #3 on: September 25, 2012, 08:20:39 pm »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: FPC\Lazarus Library for JSON
« Reply #4 on: September 26, 2012, 12:22:55 am »
Thanks for the pointers guys.

I've spent the evening looking at these, and the various libraries of fpjson, jsonparser, jsonscanner and examples such as
http://fossies.org/unix/misc/fpcbuild-2.6.0.tar.gz:a/fpcbuild-2.6.0/fpcsrc/packages/fcl-json/examples/parsedemo.pp and
https://bitbucket.org/reiniero/fpctwit/src/1c14d1542918/twitter.pas and
http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/tools/jsonviewer/?root=lazarus&sortby=date and
http://fossies.org/unix/misc/fpcbuild-2.6.0.tar.gz:a/fpcbuild-2.6.0/fpcsrc/packages/fcl-json/src/README.txt

and I'm totally confused!

Having read the entry at http://delphitools.info/2012/09/17/spotlight-on-dwsjson/ , that looks to be the most easy to follow, but I wanted to avoid using "non-lazarus\non-FPC" libraries if I could, just to keep things simple.

Having read all of these simingly quite advanced examples though, I'm still unsure how I would go about even starting what I need to do. My program reads a file in buffers that is then assigned to a string variable. I then need to look at each variable to see if there is JSON data in it. If there is, I need to say "Parse this string variable for the JSON data and access the elements that you find".

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: FPC\Lazarus Library for JSON
« Reply #5 on: September 26, 2012, 07:04:53 am »
The specific function in the fpctwit code does what you need, I think?
If you don't understand it, read it again.

I would anyway suggest focussing on 1 demo project/code snippet, go through the code to see what happens, including how the json code works.
That code snippet need not even be Leledumbo's, ludob's and my fpctwit code :)

Reading too many examples superficially is sure to confuse people.

If you then have specific questions, I'd suggest you post them. Otherwise, I don't think there's much we can do.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: FPC\Lazarus Library for JSON
« Reply #6 on: September 26, 2012, 12:04:17 pm »
When you say 'the specific function', which of the two dozen or so functions in twitter.pas do you mean, please? Could it be:

function TTwitter.ProcessTweets or
procedure ParseSingleTweet(TheTweet: TJSONObject);

from twitter.pas?

Whilst I understand your frustration at being asked seemingly stupid questions, I'm not sure "if you don't understand it, read it again" is overly helpful to those who are trying to get to grips with new programming aspects in the few spare hours they have each week. We're not all pro's. Believe it or not, but I do try to avoid wasting peoples time by only asking a question several hours after scratching my head and not coming up with an answer, in the hope that a simple none multiple function minimally nested example that doesn't make other calls to several different libraries and type defintions that confuse the hell out of me would be quite easy for someone in the know to just throw in, to get me started and help explain how the JSON library(s) work.

My very limited pseudo code example to explain how my mind is thinking is
Code: [Select]
uses
  fpjson // or whatever library is best

var
MyString : string
JSONVar : TJSONObject;

begin
// Code that reads my raw file and puts the data into a string is here
....
if JSONDataExists(MyString) then
  begin
    JSONVar := JSONParser(MyString) // Parse the MyString var and put all the elements into JSONVar
    // Now access each element of JSONVar
    Memo1.Lines.Add := JSONVar.Element1;
    // etc
  end;
end;
« Last Edit: September 26, 2012, 12:24:47 pm by tedsmith »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: FPC\Lazarus Library for JSON
« Reply #7 on: September 26, 2012, 12:25:55 pm »
- see e.g. ProcessTweets in twitter.pas in that repository for examples on decoding json. the plurk example client code has a lot of json, too

As for the pro remark: do you think I'm a pro (even if I were - would I be a pro all the time)? I wrote my previous post because I've often done what I thought you did: read many examples that use many different functions/framework and get confused. If my solution doesn't work for you, I'm sorry, but I didn't mean it to be offensive.

What I remember from FPJSON: you have to parse going from the outside in: if you have an array/list/whatever it's called that contains strings, first check for/parse a list.
Your jsondataexists in the pseudo code can be omitted - you simply check for the outer array etc parsing to work. If it doesn't, it's not the data you're looking for...

HTH
« Last Edit: September 26, 2012, 12:33:47 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: FPC\Lazarus Library for JSON
« Reply #8 on: September 26, 2012, 02:53:55 pm »
BC :

Quote
do you think I'm a pro?
- I assumed you were, or pretty high up there in the experience wing! You seem to know an awful lot but maybe you're just good at the coding!

My apologies if I came across as rude or dismissive - I reacted a little firey as I often seem to 'waste' hours (I realise 'learning' is not 'wasting' but it seems like it when you don't get any further forward) and then when asking for help, if I get no further forward, it frustrates me. I know it shouldn't, but I'm not as patient as I need to be, to be a programmer!

I'll look again at your functions and see if I can work things out from them. Thanks a lot.

Ted

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPC\Lazarus Library for JSON
« Reply #9 on: September 26, 2012, 07:07:08 pm »
Go here instead (or the corresponding directory in your installation): http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/fcl-json/examples/

 

TinyPortal © 2005-2018