How do I populate a JSON array with values (not pairs) in Delphi? -
i'm trying create json looks this:
{ "guestlist": ["alice","bob","charlie"] }
typical examples see populating json array this:
var jsobj: tjsonobject; jso : tjsonobject; jsa : tjsonarray; jsp : tjsonpair; begin jsobj := tjsonobject.create(); jsa := tjsonarray.create(); jsp := tjsonpair.create('guestlist', jsa); jsobj.addpair(jsp); jso := tjsonobject.create(); jso.addpair(tjsonpair.create('person', 'alice')); jsa.addelement(jso); jso := tjsonobject.create(); jso.addpair(tjsonpair.create('person', 'bob')); jsa.addelement(jso); jso := tjsonobject.create(); jso.addpair(tjsonpair.create('person', 'charlie')); jsa.addelement(jso); end;
but result in this:
{ "guestlist": [{"person":"alice"},{"person":"bob"},{"person":"charlie"}] }
how can add single value array instead of pair? see nothing in documentation tjsonobject
on how this,
this lot simpler you're making out be. tjsonarray
can happily contain tjsonvalue
elements solution quite straightforward.
program project1; {$apptype console} uses json; var ljobj : tjsonobject; lguestlist : tjsonarray; begin lguestlist := tjsonarray.create(); lguestlist.add('alice'); lguestlist.add('bob'); lguestlist.add('charlie'); ljobj := tjsonobject.create; ljobj.addpair(tjsonpair.create('guestlist', lguestlist)); writeln(ljobj.tostring); readln; end.
produces output :
{"guestlist":["alice","bob","charlie"]}
Comments
Post a Comment