Filter text in Delphi

Asked

Viewed 153 times

3

How to filter the 1 of "status":1, of the text below using Delphi:

{"status":1,"data":"47281274","msg":"SUCESSO"}

I tried some routines of StrReplace, but to no avail!

1 answer

4

What you can use are the classes responsible for manipulating JSON in Delphi, this text you passed is nothing more than a JSON (Javascript Object Notation).

Documentation on JSON on the Embarcadero website: http://docwiki.embarcadero.com/RADStudio/XE5/en/JSON

Example manipulating JSON:

https://stackoverflow.com/questions/24815625/parsing-valid-json-with-tjsonobject-using-embarcadero-code-example-fails-with-ex

Example I created using the text you posted, taking Status only and returning a Showmessage event with the value contained in Status.

procedure showMessageWithStatus;
var 
    vJSONString : string;
    vJSONPair : TJSONPair;
    vJSONScenario: TJSONObject;
    vParseResult : Integer;
    vJSONScenarioEntry: TJSONValue;
    vJSONScenarioValue: string;
begin
    vJSONString := '{"status":1,"data":"47281274","msg":"SUCESSO"}';
    try
        vJSONScenario := TJSONObject.Create;
        vParseResult := vJSONScenario.Parse(BytesOf(vJSONString),0);
        if  vParseResult >= 0 then
        begin
            vJSONPair := vJSONScenario.Get('status');
            vJSONScenarioEntry := vJSONPair.JsonValue;
            vJSONScenarioValue := vJSONScenarioEntry.Value;
            ShowMessage(vJSONScenarioValue);  
        end;
    finally
        vJSONScenario.Free;
    end;
end;

Any doubt we are available.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.