-2

I need addPair() to JSON object where the value is a trailing blackslash /. The result is beeing equals to this: "\/".

Then, how remove the left trailing blackslash inserted by JSON?


Edition:

Tested StringReplace() and not works.

uses
 System.JSON, REST.Json;

//...

function FormatJSON(json: string): string;
var
  tmpJson: TJsonValue;
begin
  tmpJson := TJSONObject.ParseJSONValue(json);
  Result := TJson.Format(tmpJson);
  FreeAndNil(tmpJson);
end;

function xxxxxx: String;
var
  TempObject: TJSONOBject;
  TempArray: TJSONArray;
begin
 TempObject := TJSONOBject.Create;
 TempArray := TJSONArray.Create;
 TempObject.AddPair('value', '/');
 TempArray.AddElement(TempObject);
 Result := FormatJSON(TempArray.ToJSON);
end;
FLASHCODER
  • 1
  • 7
  • 24
  • It is unclear what the actual problem is. Please edit your question to clarify, and include the actual JSON you are having trouble with. – Remy Lebeau Jan 17 '20 at 23:29
  • Why are you converting the `TJSONArray` to a `string` just to convert it back to a `TJSONValue`? `TJSONArray` derives from `TJOSNValue`, so just pass the `TJSONArray` as-is directly to `TJson.Format()`. Also, your `xxxxxx()` function is leaking the `TJSONArray`. Try this: `function xxxxxx: String; var TempObject: TJSONObject; TempArray: TJSONArray; begin TempArray := TJSONArray.Create; try TempObject := TJSONObject.Create; try TempObject.AddPair('value', '/'); TempArray.AddElement(TempObject); except TempObject.Free; raise; end; Result := TJson.Format(TempArray); finally TempArray.Free; end;` – Remy Lebeau Jan 17 '20 at 23:33
  • @RemyLebeau, thank you by this fix, but the extra trailing blackslash \ still is iserted before the real value (**/**). How prevent this? – FLASHCODER Jan 17 '20 at 23:44
  • 3
    `"\/"` is simply the *escaped* form of the `'/'` character. This is a perfectly legal escaping, per the [JSON spec](https://www.json.org/json-en.html). There is no way to tell Embarcadero's JSON framework not to escape the `'/'` character. That being said, `TJson.Format()` is deprecated in 10.3 Rio, so why are you using it at all? Use `Result := TempArray.Format();` instead. – Remy Lebeau Jan 17 '20 at 23:52
  • @RemyLebeau, on [EditThisCookie](https://chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg?hl=pt-BR) chorme extension for example, not happens it. – FLASHCODER Jan 17 '20 at 23:54
  • Obviously, the "EditThisCookie" extension is not using Embarcadero's JSON framework. Different implemetnations, different outputs. Embarcadero's output is not wrong or illegal, just different than what you are expecting. That being said, [`TJson.Format()`](http://docwiki.embarcadero.com/Libraries/en/REST.Json.TJson.Format) is deprecated in 10.3 Rio, so why are you using it at all? `TJSONArray` has its own [`Format()`](http://docwiki.embarcadero.com/Libraries/en/System.JSON.TJSONAncestor.Format) method, use `Result := TempArray.Format();` instead – Remy Lebeau Jan 17 '20 at 23:57
  • @RemyLebeau, about *EditThisCookie* chorme extension > [see](https://prnt.sc/qpaqdk). – FLASHCODER Jan 17 '20 at 23:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206175/discussion-between-remy-lebeau-and-browjr). – Remy Lebeau Jan 18 '20 at 00:01
  • Why do you want to *remove* this escape sign? `"\/"` is perfectly valid json for a string containing only `/` - character names as *solidus* in https://www.json.org/json-en.html - see https://stackoverflow.com/a/1580682/458259 – Arnaud Bouchez Jan 18 '20 at 17:55

1 Answers1

2

You may try this:

Result := FormatJSON(TempArray.ToJSON.Replace('\/', '/'));

JanL1980
  • 59
  • 1
  • 4