Answer the question
In order to leave comments, you need to log in
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.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question