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
Name | Type | Description |
---|---|---|
info | object | The additional info. |
type | string | The additional info type. |
ErrorDetail
Name | Type | Description |
---|---|---|
additionalInfo | ErrorAdditionalInfo[] | The error additional info. |
code | string | The error code. |
details | ErrorDetail[] | The error details. |
message | string | The error message. |
target | string | The 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]}