A
A
artem782017-01-27 15:49:16
Delphi
artem78, 2017-01-27 15:49:16

How to parse json through dbxjson received from synapse?

I receive data via Synapse in UTF8 encoding. Then it must be run through the StripNonJson function and "shove" it 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.

Kryakozyabras come out on the screen and naturally the json variable is empty.
79684b1caf4944b9a2cfa2691d716ef6.png
As I understand it, you need to convert Document from utf8 to Delphi 2010 native unicode, but I don't know how to do it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question