Receive JSON string via Socket

Asked

Viewed 241 times

1

I’m getting a stream with JSON strings and I’m showing on a Tmemo. It happens that I am not able to process the JSON correctly because not always comes the same amount of characters from the server. I would need to process the JSON and display the results in another TMEMO. I’ve heard of buffer, 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 Customer’s 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 starts coming line by line but sometimes everything comes together without line breaks. I need 1 JSON at a time otherwise the system does not process.

  • What else is the least JSON structure? Adds a JSON answer to the question

  • See if this helps you’ll need something like this: https://answall.com/questions/237277/replacer-palavras-ao-importa-no-listview/237354#237354

  • Exactly as @Tmc mentioned, post the structure of JSON that is receiving, if it contradicts this very broad your question!

  • @Tmc I edited the question with the return of JSON

1 answer

0


With the following code will organize JSON this way, 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 pasted the JSON response later in the onclick event of the button I created the code to handle the text, and 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 only separately, so:

{ "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" }

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.

  • It looks good, but there are still some adjustments. I’ll send the screens with the results. http://www.menq.com.br/erro.jpg (here you are right) http://www.menq.com.br/erro1.jpg (here you need to adjust). In the second image it seems that there are still some keys to remove.

  • Using the second hint is like http://www.menq.com.br/erro2.jpg strange is that sometimes closes the last key of JSON and sometimes does not close.

  • I was thinking... it could be the server that sends incomplete strings. suddenly one way would be to use buffer and release only when it has all the shipment size. but would not know how to do that.

  • @Ezequieltavares there is a possibility that the server is "eating" characters or seeing separately the ideal to understand what is happening was to download everything in one memo then do the treatment for another and see the result if it is correct

  • Actually when I click to open the connection it sends everything that is stored and error. Then he sends the separate results and then he recognizes it’s cool.

  • Thanks... with a few more changes went all right!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.