RTC Forums
May 04, 2024, 04:24:19 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Return JSON null value  (Read 3893 times)
jeff.lott
RTC Expired
*
Posts: 11


« on: December 15, 2015, 06:59:58 PM »

Let's say I want to return JSON such as:
{
  "Name" : "Fred Smith",
  "Age" : null
}

NOT: "Age" : "null"
BUT: "Age" : null

procedure xyz(Sender: TRtcConnection);
Var
  Srv:TRtcDataServer absolute Sender;
  Result: TRtcRecord;
Begin
  ..
  Result := TRtcRecord.Create;
  Try
    Result.AsString['Name'] := 'Fred Smith';
    Result.Huh['Age'] := Huh
    Srv.WriteEx(Result.toJSONEx);
  Finally
    FreeAndNil(Result);
  End;
  ...
End

How would I get value of "Age" to be just plain null

Thanks, Jeff
Logged
jeff.lott
RTC Expired
*
Posts: 11


« Reply #1 on: December 15, 2015, 08:19:05 PM »

This seems to work correctly:

  Result.asObject['Age'] := TRtcValue.Create;

Is the the "proper" way to do this, or is there a better way?

Thanks, Jeff
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #2 on: December 15, 2015, 11:44:23 PM »

That would be one way of doing it.

Another way would be to do it like this:

    Result.asValue['Age'] := Null;

Or (when Result is a TRtcRecord) like this:

    Result['Age'] := Null;

Best Regards,
Danijel Tkalcec
Logged
jeff.lott
RTC Expired
*
Posts: 11


« Reply #3 on: December 17, 2015, 05:21:47 PM »

Ahhh Yes.
I didn't include the Variants unit in my "uses" list.
So I was getting "undeclared identifier NULL" from the compiler when using Result.asValue['Age'] := Null;
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #4 on: December 18, 2015, 10:02:17 AM »

Yes. Vaiants unit is required for "Null".

Or ... you can do it like this, without using the Variants unit:

 Result.asObject['Age'] := nil;

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.025 seconds with 17 queries.