Receiving string JSON via Socket

1

I'm getting a stream with JSON strings and I'm displaying it in a TMemo. It happens that I am not able to process JSON correctly because it does not always come with the same amount of characters from the server. You would need to process the JSONs and display the results in another TMemo. I've heard of buffering, streams etc but I'm not familiar with them.

Note: Use TClientSocket

procedure TForm1.csClienteRead(Sender: TObject; Socket:  TCustomWinSocket);
var
 data: String;
begin
 data := Socket.ReceiveText;
 memoResults.Lines.Add(data);     
end;

As soon as I connect the Client connection, I receive this data:

{ "age" : "0", "camera" : "0", "direction" : "===", "id" : "12", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328838" }
{ "age" : "0", "camera" : "0", "direction" : "===", "id" : "11", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328838" }
{ "age" : "1", "camera" : "0", "direction" : "===", "id" : "10", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328837" }
{ "age" : "1", "camera" : "0", "direction" : "===", "id" : "9", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328837" }
{ "age" : "2", "camera" : "0", "direction" : "===", "id" : "8", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328836" }
{ "age" : "2", "camera" : "0", "direction" : "===", "id" : "7", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328836" }
{ "age" : "3", "camera" : "0", "direction" : "===", "id" : "6", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328835" }
{ "age" : "3", "camera" : "0", "direction" : "===", "id" : "5", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328835" }
{ "age" : "4", "camera" : "0", "direction" : "===", "id" : "4", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328834" }
{ "age" : "4", "camera" : "0", "direction" : "===", "id" : "3", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1508328834" }
{ "age" : "16402735", "camera" : "0", "direction" : ">>>", "id" : "0", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1491926103" }
{ "age" : "16402735", "camera" : "0", "direction" : "===", "id" : "2", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1491926103" }
{ "age" : "16402736", "camera" : "0", "direction" : "===", "id" : "1", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1491926102" }
{ "age" : "16402736", "camera" : "0", "direction" : "===", "id" : "0", "plate" : "DEMOPLATE", "strength" : "0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName" : "AUTOPARKING", "timestamp" : "1491926102" }

After this begins to come line by line but sometimes it all comes together without line breaks. I need 1 JSON at a time otherwise the system does not process.

    
asked by anonymous 18.10.2017 / 02:12

1 answer

0

With the following code you will organize the JSON in this way, for example:

  

age: 0 - camera: 0 - direction: === id: 12
plate: DEMOPLATE
strength: 0.66 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00 - systemName : AUTOPARKING
timestamp: 1508328838

I used two memos and a button for the code to work, in memo1 I pasted the JSON response then in the onClick event of the button I created the code for handling the text, and I write in memo2.

Code:

var APos: Integer;
    AText, AWord, AResult: String;
begin
  AResult := '';

  AText := Memo1.Text;
  While (Trim(AText) <> '') do
    Begin
      APos := Pos('"', AText);
      if (APos > 0) then
        begin
          AWord := Trim(AnsiMidStr(AText, 1, APos - 1));
          Delete(AText, 1, APos);
        end
      else
        Begin
          AWord := Trim(AText);
          AText := '';
        End;

      if (AWord <> ',') and (AWord <> '{') and (AWord <> '}') and (AWord <> '} {') then
        Begin
          AResult := AResult + AWord;
          AWord := '';
        End;

      if (AWord <> '') then
        Begin
          Memo2.Lines.Add(AResult);
          AResult := '';
        End;
    End;
End;

If you need the answers just separated, like this:

  

{"age": "0", "camera": "0", "direction": "===", "id": "12", "plate": "DEMOPLATE" "0.6 - 1.00 0.90 1.00 0.66 1.00 0.90 1.00", "systemName": "AUTOPARKING", "timestamp": "1508328838"}

Use the following code:

var APosB, APosE: Integer;
    AText, AResult: String;
begin
  AResult := '';
  AText := Memo1.Text;
  While (Trim(AText) <> '') do
    Begin
      APosB := Pos('{', AText);
      APosE := Pos('}', AText);
      if (APosB > 0) and (APosE > 0) then
        begin
          AResult := Trim(AnsiMidStr(AText, APosB, APosE - 1));
          Delete(AText, 1, APosE);
        end
      else
        Begin
          AResult := Trim(AText);
          AText := '';
        End;

      Memo2.Lines.Add(AResult);
    End;
end;

Any questions in the code let me know.

    
18.10.2017 / 17:13