Jump to content
JIMSMITH

clear a tjsonobject

Recommended Posts

Is there a way to remove pairs, as in all pairs, from a tjsonobject?  Looking for something like tjsonobject.clear.

Share this post


Link to post

There is:

function TJSONObject.RemovePair(const PairName: string): TJSONPair;
 

To clear all pairs:

JsonObject.SetPairs([]);

 

You could also Free the JsonObject and create a new one.

Share this post


Link to post

If I try the setpairs above I receive the message below when compiling.

 

[dcc32 Error] UfrmChannelConfig.pas(161): E2010 Incompatible types: 'System.Generics.Collections.TList<System.JSON.TJSONPair>' and 'Set'

Share this post


Link to post
54 minutes ago, JIMSMITH said:

Is there a way to remove pairs, as in all pairs, from a tjsonobject?

Not "out of the box". You could perhaps do this:

while LJSONObject.Count > 0 do
  LJSONObject.RemovePair(LJSONObject.Pairs[0].JsonString.Value).Free;

 

Share this post


Link to post
1 hour ago, pyscripter said:

To clear all pairs:

JsonObject.SetPairs([]);

That won't work. SetPairs() expects a list, not a Set or an array.

 

You would need to use this instead:

JsonObject.SetPairs(TList<TJSONPair>.Create);

This is because SetPairs() takes ownership of the new list, freeing the old list. The list can't be nil or else you risk crashes on future operations on the TJsonObject.

Edited by Remy Lebeau
  • Like 2

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

×