Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread JSON and the TStringList
Wed, Mar 13 2013 11:22 AMPermanent Link

Matthew Jones

I thought this might be handy for others, so I'm posting it here. It takes a
TStringList that has name/value pairs, and makes a JSON string from them. And then
will restore them back. Based on the EWB code, all errors mine. If anyone improves
on it, let me know!

(No need to comment on my odd "hungarian" notation. 8-)

/Matthew Jones/
function StringListToJSON(xList : TStrings) : String;
var                  
   szName : String;
   szValue : String;
   bAddComma : Boolean;
   nLoop : Integer;
begin        
   bAddComma := false;
   Result:='{ ';
   for nLoop := 0 to xList.Count - 1 do
   begin           
       szName := Trim(xList.Names[nLoop]);
       if szName <> '' then
       begin
           szValue := xList.ValueFromIndex[nLoop];
           if bAddComma then
               Result := Result + ', ';
           Result := Result+'"' + szName + '": "' + EscapeStr(szValue) + '"';
       end;           
       bAddComma := true;
   end;
   Result:=Result+' }';
end;


procedure JSONToStringList(szDataString : String; xList : TStrings);
var
   szName : String;
   szValue : String;
   bAddComma : Boolean;
   nLoop : Integer;
   xParser: TParser;
begin        
   bAddComma := false;

   xList.Clear;
   xParser := TParser.Create;
   try    
       xParser.Initialize(szDataString, false);
       xParser.ErrorIfNotSkipToken('{');
       while true do
       begin
           xParser.ErrorIfNotToken(tkString);
           szName := xParser.TokenString;
           xParser.NextToken;
           xParser.ErrorIfNotSkipToken(':');
           xParser.ErrorIfNotToken(tkString);
           szName := xParser.TokenString;
           xParser.NextToken;
           xList.Values[szName] := szValue;
           if (not xParser.SkipToken(',')) then
              Break;
       end;
       xParser.ErrorIfNotSkipToken('}');
   finally
       xParser.Free;
   end;
end;
Wed, Mar 13 2013 12:46 PMPermanent Link

Matthew Jones

This appears not to work! At least in reading. I will be fixing it momentarily.
This is better - assign the value instead of the name twice!

procedure JSONToStringList(szDataString : String; xList : TStrings);
var
   szName : String;
   szValue : String;
   bAddComma : Boolean;
   nLoop : Integer;
   xParser: TParser;
begin        
   bAddComma := false;

   xList.Clear;
   xParser := TParser.Create;
   try    
       xParser.Initialize(szDataString, false);
       xParser.ErrorIfNotSkipToken('{');
       while true do
       begin
           xParser.ErrorIfNotToken(tkString);
           szName := xParser.TokenString;
           xParser.NextToken;
           xParser.ErrorIfNotSkipToken(':');
           xParser.ErrorIfNotToken(tkString);
           szValue := xParser.TokenString;
           xParser.NextToken;
           xList.Values[szName] := szValue;
           if (not xParser.SkipToken(',')) then
              Break;
       end;
       xParser.ErrorIfNotSkipToken('}');
   finally
       xParser.Free;
   end;
end;


/Matthew Jones/
Wed, Mar 13 2013 4:20 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< This appears not to work! At least in reading. I will be fixing it
momentarily. This is better - assign the value instead of the name twice! >>

Thanks, nice work. Smile

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Apr 26 2013 10:56 AMPermanent Link

Matthew Jones

> procedure JSONToStringList(szDataString : String; xList : TStrings);

If anyone gets to using these, let me know. I've enhanced this to make the lists
named, so you can store multiple lists in the single JSON. Well, at the moment my
Delphi server can generate them, and the EWB code just picks the one it wants, but
by the time anyone asks I may have it working both ways. I am moving to the simple
list being in an array too, rather than a list of pairs.

/Matthew Jones/
Fri, Apr 26 2013 10:45 PMPermanent Link

cwsymons

Hi Matthew,
 I use your code, works well.

Thank you

regards


Craig Symons
Image