Jump to content
PizzaProgram

How to build JSON with ISuperObject as an Array ?

Recommended Posts

My code looks currently like this, but it is causing AV error. 😞

function JSONt_array_Creator(z1: string) : SOString;
var
    o, n: ISuperObject;
begin
    Result := '';
    o := SO();
    insert_other_JSON_data( o );

    n := SO();
    n.S['id'] := z1 ;
    o.O['identifiers'].AsArray.Add( n );  // << ERROR

    Result := o.AsJSon();
    n := nil; o := nil;
end;

The result should look like this

{
  "base": "something",
  "identifiers": 
  [
    {id: 2}
  ]
}

 

There are many many SO examples out there, but all about "how to parse / read / digest ..." not a single one about "how to CREATE SO sub-tree or array ... ".

 

Edited by PizzaProgram

Share this post


Link to post

ISuperObject documentation and examples are very poor, but it's a reliable and fast library...

 

There is an example in the OverbyteIcsSslX509Certs.pas unit, search for SA([]) which creates an empty array, then you can add elements with a blank name to create a simple array.

 

Angus

 

 

 

  • Thanks 1

Share this post


Link to post
1 hour ago, PizzaProgram said:

This method works: 🙂

Is your problem solved?

If not, on which line EXACTLY does your code cause an AV?

Run your program under the debugger using the debug release.

Share this post


Link to post
5 minutes ago, FPiette said:

Is your problem solved?

YES, thank You! 🙂

 

OFF:

I think the problem was, that first we need to create an Array on that level, and we can .Add() elements only after that.

 

Maybe you can improve the code later to add "auto-array-creation" in those cases, but it's not necessary.

Some better examples can do it also.

 

I would do it myself, but I have a nearing deadline and already working 14-16 hours pro day. Sorry.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×