May 3, 2024

Many web services or external JavaScript scripts return JSON notation.

You can use EWB’s parser to parse them into objects, which is what EWB does itself to parse the components. And I’ve seen a lot of people stuggle with thta. But configuring its parse is a lot of work and there is an easier way.

JavaScript includes two particularly useful functions to convert Objects into strings, and strings into objects.

Type

  external TJSON = class( TExternalObject )
  public
     function stringify( obj : variant ): string;
     function parse( s : string ): variant;
  end;

var
  external JSON TJSON;

Now, to generate a JSON string from any object, just use this:

var
  s : string;
  dummy : TExternalObject;
  array : array of string;
  ...
  // convert an object
  s := JSON.stringify( dummy );
  // or an array
  s := JSON.stringify( array );

That’s handy if you wish to upload an Object (or EWB class or array) to a Web service.

And to read a JSON string back into an object (and subobjects) just use:

  dummy := JSON.parse(s);