#1 2024-03-07 02:00:53

loadymf
Member
Registered: 2023-10-20
Posts: 4

How to converting JSON to class

I have a program that returns JSON data after a login request.(I use laz 2.2.4 fpc 3.2.2 mormot2)
Successful login returns JSON data,Other responses in the "datas" field will return multiple pieces of data

{"status":"200","flag":"SUCCESS","message":"操作成功",' "action":"safetyLogin","version":"v1.0","format":"json","datas": [{"sessionUuid":"0cdb276a98a24d03aad4814e9d059781","tenantCountFlag":2}]}

Login failure returns JSON data

{"action":"safetyLogin","version":"v1.0","format":"json","status":"500",' +
    '"flag":"NOT_FOUND","message":"帐号尚未注册"}

an other respone datas data :
{"actived":0,"address":"Dh,"companyName":"dhxxx",'+
  '"domain":"jyj","employeeName":"loadymf","logoUuid":"0080g974ae783d153214514",'+
  '"tenantAccountId":4864,"tenantId":1079,"tenantType":4,"wxwCorpId":"wpbY5x"'+
  ',"wxwRealCorpId":"ww64df1a47"}

//class define
  TResponData = class
  private
    FAction: string;
    FDatas: string;
    FFlag: string;
    FFormat: string;
    FMessage: string;
    FStatus: string;
    FVersion: string;
 
  protected

  published
    property Action: string read FAction write FAction;
    property Datas: string read fDatas write FDatas;
    property Flag: string read FFlag write FFlag;
    property Format: string read FFormat write FFormat;
    property Message: string read FMessage write FMessage;
    property Status: string read FStatus write FStatus;
    property Version: string read FVersion write FVersion;
  public
  end;

//
procedure TForm1.Button1Click(Sender: TObject); 
var
  MjStr, mStr: string;
rd : TResponData;
begin
//{
  mjstr := '{"status":"200","flag":"SUCCESS","message":"操作成功",' +
    '"action":"safetyLogin","version":"v1.0","format":"json","datas":' +
  '[{"sessionUuid":"0cdb276a98a24d03aad4814e9d059781","tenantCountFlag":2}]}';
//}
{  //
 mjstr:= '{"action":"safetyLogin","version":"v1.0","format":"json","status":"500",' +
    '"flag":"NOT_FOUND","message":"帐号尚未注册"}';
//}
 rd := TResponData.Create;
  mormot.core.json.ObjectLoadJson(rd, mjstr);
  showmessage(rd.Message) ;//disp OK
   showmessage(rd.Datas) ;//disp nil  ,I cannot to use ObjectLoadJson to load subclass or array class

Lazarus 2.24 FPC3.2.2

Offline

#2 2024-03-07 02:06:31

loadymf
Member
Registered: 2023-10-20
Posts: 4

Re: How to converting JSON to class

  TCompanyData = packed record
    Actived: integer;
    Address: string;
    CompanyName: string;
    Domain: string;
    EmployeeName: string;
    LogoUuId: string;
    TenantAccountId: integer;
    TenantId: integer;
    TenantType: integer;
    WxwCorpId: string;
    WxwRealCorpId: string;
  end; 

var
rd:TCompanyData;
Mjstr:string;
begin

MjStr:='{"actived":0,"address":"Dh,"companyName":"dhxxx",'+
  '"domain":"jyj","employeeName":"loadymf","logoUuid":"0080g974ae783d153214514",'+
  '"tenantAccountId":4864,"tenantId":1079,"tenantType":4,"wxwCorpId":"wpbY5x"'+
  ',"wxwRealCorpId":"ww64df1a47"}

   mormot.core.json.RecordLoadJson(rd,pointer(MjStr),typeinfo(rd));
  ShowMessage(rd.CompanyName);//disp null  ,I found ,rd load nothing;

Lazarus 2.24 FPC3.2.2

Offline

#3 2024-03-07 08:41:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: How to converting JSON to class

By definition, you can't load an array into a string.
Use a RawJson type instead.

On FPC/Lazarus, you need to define the record layout with a text definition in your code, because it lacks the need RTTI (which is available since Delphi 2010).

Offline

Board footer

Powered by FluxBB