I receive data via Synapse in UTF8 encoding. Then it should be run through the function StripNonJson and "shoved" into a variable of type TJsonObject.

function StripNonJson(s: string): string; // Убирает лишние пробелы из json-строки var ch: char; inString: boolean; begin Result := ''; inString := false; for ch in s do begin if ch = '"' then inString := not inString; if TCharacter.IsWhiteSpace(ch) and not inString then continue; Result := Result + ch; end; end; function MemoryStreamToString(M: TMemoryStream): string; begin SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char)); end; var json: TJSONObject; // ... begin // ... with THTTPSend.Create do begin try MimeType := 'application/x-www-form-urlencoded'; Document.LoadFromStream(data); if HTTPMethod('POST', 'http://httpbin.org/post') then begin WriteLn('res=' + MemoryStreamToString(Document)); json := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StripNonJson(MemoryStreamToString(Document))), 0) as TJSONObject; if Assigned(Json) then begin WriteLn('Json parsed!'); end else begin WriteLn('Error parsing JSON!'); end; end else begin WriteLn('Request error: ' + IntToStr(ResultCode) + ' ' + ResultString); end; finally Free; end; end; end. 

The screen quackers come out and of course in the json variable is empty.

I understand you need to translate Document from utf8 into native Delphi 2010 unicode, but I don’t know how to do it.

    1 answer 1

    In Delphi there is a class TEncoding for converting strings between encodings

     function MemoryStreamToString(M: TMemoryStream): string; begin Result := TEncoding.UTF8.GetString(M.Memory, 0, M.Size); end; 
    • in addition, most likely, for output to the console (and only for it) it will be necessary to use AnsiToOEM. - kami
    • @kami will not need - Anton Shchyrov