Search code examples
jsondelphi-2007

Delphi 2007 JSON array of string to TStringList


Embarcadero introduced JSON types with Delphi 2009, but I'm using Delphi 2007 without these JSON types.

I'm wanting to process a JSON string being an "array of strings" into a TStringList object.

e.g. ["Ford", "BMW", "Fiat"]

I guess it's string token type of processing ..


Solution

  • There is a third party Delphi unit that might be useful for this:

    https://sourceforge.net/projects/lkjson/

    uses uLkJSON;
    
    var
      AString:String;
      AStringList:TStringList;
      jsonbase,Items : TlkJSONbase;    //as per 3rd party unit uLkJSON
      I: Integer;
    begin
        AStringList:=TStringList.create;
        AString := '["Ford", "BMW", "Fiat"]';
        jsonbase := TlkJSON.ParseText(AString);
        if Assigned(jsonbase) then
        begin
            for I := 0 to Pred(jsonbase.Count) do
            begin
                Items := jsonbase.Child[I];
                AStringList.add(Items.Value);
            end;
        end;
        Result := AStringList;
    end;