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.
