I need to prepare JSON and send it to a website. I am using TJSONObject. My code is simple:
procedure TForm1.Button1Click(Sender: TObject);
var
JsonArray,JsonArray1:TJSONArray;
F,F1:TJSONObject;
begin
FJSONObject.AddPair('api_password','password');
FJSONObject.AddPair('method','POST');
F:=TJSONObject.Create;
F.AddPair('nest1','v1');
F.AddPair('nest2','v2');
JsonArray:=TJSONArray.Create;
JsonArray.AddElement(F);
FJSONObject.AddPair('Main array',JsonArray);
end;
As a result I've got this JSON:
{
"api_password": "password",
"method": "POST",
"Main array": [
{
"nest1": "v1",
"nest2": "v2"
}
]
}
But, according to the website`s API, I need to send this JSON instead:
{
"api_password": "password",
"method": "POST",
"Main array": [
{
\"nest1\": \"v1\",
\"nest2\": \"v2\"
}
]
}
How can I make this JSON?
TJSONObject.ToStringautomatically adds escape characters where needed. I don't have a deep knowledge about that but I think that your site's API doesn't expect a standard JSON string"characters escaped as shown. Only"characters inside of strings can be escaped.