Recent

Author Topic: Using JSON  (Read 19379 times)

Kips

  • New Member
  • *
  • Posts: 27
Using JSON
« on: August 30, 2011, 11:06:27 am »
Hi,


Does anyone have an example on how to parse data with JSON? I am very new to JSON...

The following code gives an exception: EJSON, Cannot convert data from object value.


uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  fpjson;


// ...


procedure TForm1.Button1Click(Sender: TObject);
var
  json    : TJSONObject;

begin
  json    := TJSONObject.Create;
  try
    json.Add('Age',32);
    Memo1.lines.text := json.AsString;       
  finally
    json.Free;
  end;
end;   

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Using JSON
« Reply #1 on: August 30, 2011, 01:13:15 pm »
Code: [Select]
Memo1.lines.text := json.AsJSONAsString is for getting string value of a JSON node.

Kips

  • New Member
  • *
  • Posts: 27
Re: Using JSON
« Reply #2 on: August 30, 2011, 03:30:08 pm »
Thanks!

addr3ss

  • New Member
  • *
  • Posts: 18
Re: Using JSON
« Reply #3 on: July 05, 2015, 10:23:00 am »
how can i load data from an exiting json file?

Basile B.

  • Guest
Re: Using JSON
« Reply #4 on: July 05, 2015, 10:33:16 am »
how can i load data from an exiting json file?

the classes TJSONDeStreamer and TJSONStreamer allow to stream a TComponent just like with the native format (e.g it saves/loads the published properties). They are located  in the unit fpjsonrtti.

You can have a look at this example for how to use.

addr3ss

  • New Member
  • *
  • Posts: 18
Re: Using JSON
« Reply #5 on: July 05, 2015, 10:51:23 am »
i want to call a string from json file.could you give me a simple example?

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using JSON
« Reply #6 on: July 06, 2015, 01:52:01 am »
Nice JSON tutorial by example can be found here:
http://lazplanet.blogspot.com/2014/09/a-simple-json-parsing-example.html
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

addr3ss

  • New Member
  • *
  • Posts: 18
Re: Using JSON
« Reply #7 on: July 06, 2015, 01:24:46 pm »
Nice JSON tutorial by example can be found here:
http://lazplanet.blogspot.com/2014/09/a-simple-json-parsing-example.html
It is for internal json but i have an external json file so it needs to set its path.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using JSON
« Reply #8 on: July 06, 2015, 04:05:17 pm »
Nice JSON tutorial by example can be found here:
http://lazplanet.blogspot.com/2014/09/a-simple-json-parsing-example.html
It is for internal json but i have an external json file so it needs to set its path.
Are you saying that you don't know how to load file to a stringlist and then copy text from it to another string, since everything else is like in mentioned tutorial?
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

addr3ss

  • New Member
  • *
  • Posts: 18
Re: Using JSON
« Reply #9 on: July 06, 2015, 04:22:55 pm »
Nice JSON tutorial by example can be found here:
http://lazplanet.blogspot.com/2014/09/a-simple-json-parsing-example.html
It is for internal json but i have an external json file so it needs to set its path.
Are you saying that you don't know how to load file to a stringlist and then copy text from it to another string, since everything else is like in mentioned tutorial?
Seems you did not read it:
http://forum.lazarus.freepascal.org/index.php/topic,14452.msg182179.html#msg182179

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Using JSON
« Reply #10 on: July 06, 2015, 05:29:49 pm »
TJSONParser doesn't have a TJSONParser.Create(JSON_Filename) constructor. It does have a TJSONParser.Create(TFileStream) constructor so you need to create a TFileStream from your file.

Something like this:
Code: [Select]
uses uses fpjson, jsonparser;

procedure ParseJSONFile(FileName: string);
var
  Fs: TFileStream;
  P: TJSONParser;
  J: TJSONData;
begin
  Fs := TFileStream.Create(Filename, fmopenRead);
  try
    P := TJSONParser.Create(Fs);
    try
      J := P.Parse;
      if Assigned(J) then
      begin
        //.. do your thing
      end;
    finally
      if Assigned(J) then
        J.Free;
      P.Free;
    end;
  finally
    Fs.Free;
  end;
end;

B.T.W. There are some demos in fpc\packages\fcl-json\examples. For instance the parsedemo.pp gives you exactly what you want.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using JSON
« Reply #11 on: July 07, 2015, 08:30:07 am »
Nice JSON tutorial by example can be found here:
http://lazplanet.blogspot.com/2014/09/a-simple-json-parsing-example.html
It is for internal json but i have an external json file so it needs to set its path.
Are you saying that you don't know how to load file to a stringlist and then copy text from it to another string, since everything else is like in mentioned tutorial?
Seems you did not read it:
http://forum.lazarus.freepascal.org/index.php/topic,14452.msg182179.html#msg182179
I did read it. I just thought that when I write what is needed you do some research on your own. I didn't expect that you want your free dinner served on a silver plate. Ok, this time I will make an exception...

You can load JSON to Memo using something like this:
Code: [Select]
Memo1.Lines.LoadFromFile('YourFile.json');
Then you use it the way you want. Of course, first you need to finish that tutorial, and learn more about methods available by reading help and wiki to use what is needed for your parsing. No one can do that for you. In case you want to treat loaded file as a big string for some reason, you can read it from Memo1.Lines.Text.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018