RTC Forums
November 23, 2024, 04:12:13 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Usage of JSON Parameters  (Read 5911 times)
bebeantics
RTC License+
****
Posts: 29


« on: June 30, 2018, 05:49:21 PM »

Hi  to all

Im new in Json strings and parameters and i need some help.
I made a HTTP server with RTC components. One function need to receive a JSON type parameter, like described as show below:

{
   "comanda": [{
      "Orderid": "100057",
      "Data": "2018-06-28",
      "Ora": "16:05:00",
      "Cufactura": "0",
      "CUIFactura": "0",
      "Masa": "DEL",
      "Utilizator": "0",
      "StoreNr": "0",
      "PosNr": "WWW"
   }],
   "client": [{
      "CliID": "11",
      "CliDen": "GEORGESCU G.",
      "CliStr": "UNIRII",
      "CliNR": "170",
      "CliBl": "0",
      "CliSc": "0",
      "CliEt": "0",
      "CliAp": "0",
      "CliInterfon": "0",
      "CliZona": "0",
      "CliReper": "0"
   }],
   "Continut": [{
      "idprodus": "1234688",
      "prodden": "COCA COLA",
      "cant": "2.00",
      "pretu": "5.00"
   }, {
      "idprodus": "12465",
      "prodden": "JW RED 40ML",
      "cant": "1.00",
      "pretu": "15.00"
   }]
}

On the server side i have a rtcdataprovider with some functions.
One of them will have to use this parameter.


I need to know how to define, test and use this parameter.

Thank you in advance.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: June 30, 2018, 07:05:27 PM »

If you are new to JSON, start here:
https://www.json.org/

On the RTC Server side, if you get your JSON data as part of the request content body (POST method), you can read that data as any other content using the "Sender.Read" (as a string) or "Sender.ReadEx" (as a byte array) method in the "OnDataReceived" event of your "RtcDataProvider" component. You can use the string or array you receive there with any JSON parser to access each element contained within the JSON string individually.

For example, if you KNOW that you are getting a JSON object ( JSON string enclosed in { } ) in a HTTP POST request and want to get a TRtcRecord instance containing that JSON object (TRtcRecord is also used with RTC Remote functions), you could implement the "OnDataReceived" event of your "RtcDataProvider" component like this ...

procedure myModule.myDataProvOnDataReceived(Sender:TRtcConnection);
var myJSON:TRtcRecord;
begin
if Sender.Request.Complete then
  begin
  myJSON:=TRtcRecord.FromJSON(Sender.Read);
  try
    // use myJSON to access JSON objects and arrays ...
  finally
    myJSON.Free;
    end;
  end;
end;
Logged
bebeantics
RTC License+
****
Posts: 29


« Reply #2 on: June 30, 2018, 09:44:51 PM »


Thank you for your answer, but I need something more explicit
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: July 01, 2018, 09:45:26 AM »

Generally speaking, from the "RTC SDK" perspective, when you are working with "TRtcDataProvider" components, it makes no difference what kind of "content" you get in a request or what you prepare in the response. "RTC SDK" handles the communication, while you write your own code to process the "content" you get and prepare the "content" you want to send back. IOW, even though the "RTC SDK" does have classes for parsing and generating JSON (which are used with RTC Remote Functions and the RTC Scripting Engine), their usage is entirely optional (as you can see from my reply below), which makes your question rather "off-topic".

Anyway ... if you know how to write a Server to receive a request and prepare a response (this is explained in RTC Quick Start lessons and examples) and you know how to work with JSON (this depends on the JSON library you use), then you have everything you need to process a request containing JSON data and send back a response from your RTC Server.

If you are new to JSON, you might want to start by learning about JSON (see my link above), so you can understand what you see when looking at JSON without using a JSON parser. To parse JSON, so you can work with Delphi objects instead of a plain JSON string and/or to generate JSON from Delphi objects, you have several options ...

A) If you are using a newer Delphi version, you can use the standard JSON framework included with Delphi. Google 'Delphi JSON' and you should find enough examples on the web, or ...

B) You can use RTC Value objects (TRtcValue, TRtcRecord, TRtcArray), which are included in the RTC SDK and used with RTC Remote Functions. You can learn how to work with them through RTC Quick Start lessons and examples, or ...

C) You can pick any other JSON library for Delphi, or ...

D) You can write your own JSON parser and/or generator.
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.026 seconds with 17 queries.