Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread Using TReader
Wed, Mar 17 2021 5:35 AMPermanent Link

Paul Coshott

Avatar

Hi All,

I'm having a bit of trouble getting the TReader to read the value I want. Code is below. I thought I'd have to skip the first 3 using SkipProperty, then SkipPropertyName, then SkipPropertySeparator, then the next one would be what I want. But I get the 'Unknown Error'.

Anyone know where I'm going wrong?

Thanks,
Paul


 sJson :=  '{"pCompanyId": 1, "pRosterId": 78, "pStaffId": 2, "pStaffName": "John Doe"}';

 Reader := TReader.Create;
 try
   try
     if Pos('"pstaffname":', Lowercase(sJSON)) <> 0 then begin
       Reader.Initialize(sJSON);
       Reader.BeginObject;
       Reader.SkipProperty;
       Reader.SkipProperty;
       Reader.SkipProperty;
       Reader.SkipPropertyName;
       Reader.SkipPropertySeparator;
       sMessage := Reader.ReadString;
     end else begin
       sMessage := sJSON;
     end;
   except
     sMessage := 'Unknown Error';
   end;
 finally
   Reader.Free;
 end;
Wed, Mar 17 2021 6:14 AMPermanent Link

Matthew Jones

Paul Coshott wrote:

> I'm having a bit of trouble getting the TReader to read the value I want. Code is below. I thought I'd have to skip the first 3 using SkipProperty, then SkipPropertyName, then SkipPropertySeparator, then the next one would be what I want. But I get the 'Unknown Error'.
>
> Anyone know where I'm going wrong?

I stopped using the lower level stuff and used the automatic property reading instead, but when you are using the low level, I found it essential to use the browser debugging tools to step through it and see what it was looking at at each point. And to read the code in the framework to see how it is doing things. It wasn't easy, but you end up with code that works.


--

Matthew Jones
Tue, May 4 2021 5:24 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Paul,

<< Anyone know where I'm going wrong? >>

You're not skipping the property separators (commas).  Use this instead:

      Reader.Initialize(sJSON);
      Reader.BeginObject;
      Reader.SkipProperty;
      Reader.ErrorIfNotMoreProperties;
      Reader.SkipProperty;
      Reader.ErrorIfNotMoreProperties;
      Reader.SkipProperty;
      Reader.ErrorIfNotMoreProperties;
      Reader.SkipPropertyName;
      Reader.SkipPropertySeparator;
      sMessage := Reader.ReadString;

The ErrorIfNotMoreProperties method will raise an exception if there isn't a comma there.

Of course, this is an easier way of dealing with this (at least in terms of eliminating having to count properties, etc.):

type

  TStaffPerson = class(TPersistent)
     private
        FCompanyID: Integer;
        FRosterID: Integer;
        FStaffID: Integer;
        FStaffName: String;
     published
        property pCompanyID: Integer read FCompanyID write FCompanyID;
        property pRosterID: Integer read FRosterID write FRosterID;
        property pStaffID: Integer read FStaffID write FStaffID;
        property pStaffName: String read FStaffName write FStaffName;
     end;

procedure TForm1.Button13Click(Sender: TObject);
var
  sJson: String;
  Reader: TReader;
  StaffPerson: TStaffPerson;
begin
sJson :=  '{"pCompanyId": 1, "pRosterId": 78, "pStaffId": 2, "pStaffName": "John Doe"}';
Reader := TReader.Create;
  try
   StaffPerson := TStaffPerson.Create;
   try
     Reader.Initialize(sJSON);
     StaffPerson.Load(Reader);
     ShowMessage(StaffPerson.pStaffName);
  finally
     StaffPerson.Free;
  end;
finally
  Reader.Free;
end;
end;

Tim Young
Elevate Software
www.elevatesoft.com
Image