Using a TJSONObject
, I've noticed that its AddPair
function has the following overloads:
function AddPair(const Pair: TJSONPair): TJSONObject; overload;
function AddPair(const Str: TJSONString; const Val: TJSONValue): TJSONObject; overload;
function AddPair(const Str: string; const Val: TJSONValue): TJSONObject; overload;
function AddPair(const Str: string; const Val: string): TJSONObject; overload;
In particular, I've noticed that there's no overload for adding not-string values like integers, datetimes...
Due to this reason, calling ToString
function, every value is shown as double quoted:
{"MyIntegerValue":"100"}
From what I've read in this answer, it goes against the JSON standard for not-string values.
How should a not-string value be added to a TJSONObject
?
You can use TJSONNumber
and the AddPair
overload that uses TJSONValue
to create a numeric JSON value, like so:
program Project1;
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.JSON;
var
JSON: TJSONObject;
begin
JSON := TJSONObject.Create;
try
JSON.AddPair('MyIntegerValue', TJSONNumber.Create(100));
writeln(JSON.ToString);
readln;
finally
JSON.Free;
end;
end.
Outputs {"MyIntegerValue":100}
This is also how it's done in the codesample from help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With