Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3771

How to deserialize JSON to TJsonObject in Delphi 12?

$
0
0

I am trying to deserialize a JSON response into Delphi objects using TJson.JsonToObject. The structure of the response and the classes I'm using are shown below, along with the data structure I'm working with (see the attached image).

Data Structure:

ErrorAdditionalInfo

NameTypeDescription
infoobjectThe additional info.
typestringThe additional info type.

ErrorDetail

NameTypeDescription
additionalInfoErrorAdditionalInfo[]The error additional info.
codestringThe error code.
detailsErrorDetail[]The error details.
messagestringThe error message.
targetstringThe error target.

Here is the class structure:

  TErrorAdditionalInfo = class  private    FInfo: TJSONObject;    FType: string;  public    destructor Destroy; override;    property Info: TJSONObject read FInfo;    property &Type: string read FType;  end;  TErrorDetail = class  private    FAdditionalInfo: TArray<TErrorAdditionalInfo>;    FCode: string;    FDetails: TArray<TErrorDetail>;    FMessage: string;    FTarget: string;  public    destructor Destroy; override;    property AdditionalInfo: TArray<TErrorAdditionalInfo> read FAdditionalInfo;    property Code: string read FCode;    property Details: TArray<TErrorDetail> read FDetails;    property Message: string read FMessage;    property Target: string read FTarget;  end;

When I try to deserialize the JSON string into the Delphi object using the following code:

var Err := TJson.JsonToObject<TErrorDetail>(AResponseString);

All members are deserialized correctly except for TErrorAdditionalInfo.FInfo, which remains empty. I expect this field to be populated with a TJSONObject containing the info object from the JSON response.

How can I correctly deserialize the info object as a TJsonObject in FInfo? Is it possible to achieve this using REST.Json, or is there another approach I should take (i.e. parse all these objects manually)?

The JSON I am willing to serialize looks like:

{"additionalInfo": {"info": {"Key1": "TextValue","Key2": 123,"Key3": [1, 2, 3]    },"type": "SomeType"  },"code": "some code","details": [],"message": "Hello world","target": ""}

I expect TJsonObject to contain the following data:

{"Key1": "TextValue","Key2": 123,"Key3": [1, 2, 3]}

Viewing all articles
Browse latest Browse all 3771

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>