0

Starting from a VCL project to TMS WEB Core, I can no longer use "System.JSON".

Having declared :

LJsonValue: TJsonValue;
LJsonArray: TJsonArray;
LJSonString: TJsonString;

How to translate VCL line

LJsonValue := LJsonValue.GetValue<TJSonValue>('choices');
LJSonString := LJSonArray.Items[0].GetValue<TJSONString>('text');

for TMS WEB Core? I can't find the solution not being comfortable with JSON.

L0pez
  • 11
  • 3
  • TMS Webcore has a `WEBLib.JSON` unit. Have you included this unit? – John Easley Jan 30 '23 at 13:31
  • Yes, but how to modify these two lines with GetValue? – L0pez Jan 30 '23 at 13:38
  • Are you parsing a WebHTTPRequest response? If so, I can post and example.. – John Easley Jan 30 '23 at 14:19
  • var LCb: TTMSFNCCloudBase; LPostdata: string; LJsonValue: TJsonValue; LJsonArray: TJsonArray; LJSonString: TJsonString; if Lcb.RequestResult.Success then begin LJsonValue := TJSonObject.ParseJSONValue(Lcb.RequestResult.ResultString); LJsonValue := LJsonValue.GetValue('choices'); if LJsonValue is TJSonArray then begin LJSonArray := LJsonValue as TJSonArray; LJSonString := LJSonArray.Items[0].GetValue('text'); Result := LJSonString.Value; end; end; – L0pez Jan 30 '23 at 15:04
  • Is the response an array? – John Easley Jan 30 '23 at 16:39

1 Answers1

0

Basically in TMS Web Core you can use the WEBLib.JSON-Unit. With that you can translate your VCL-Code to this:

uses WEBLib.JSON;

...  
var
  LJsonValue: TJSObject;
  LJsonValue2: TJSObject;
  LJsonArray: TJSArray;
  LString: String;
begin
  ...
  LJsonValue2 := TJSJSON.parseObject(LJsonValue.Properties['choices']);
  LString := String(TJSJSON.parseObject(LJsonArray[0]).Properties['text']);

With the function TJSJSON.parseObject from the unit JS you can parse a JSValue to a TJSObject and with that you can access all the Properties of the underlying JSON-Node.

You can also cast TJSObject to a TJSArray and iterate over it´s items.

Max
  • 41
  • 7