thomaserlang / delphi-json

JSON parser for Delphi.
83 stars 32 forks source link

Best way to parse this? #7

Closed AdriaanBoshoff closed 6 years ago

AdriaanBoshoff commented 6 years ago

Sorry I must be annoying with all my questions. What is the best way I can parse this? It's raw data straight from my server. I want to get the Display Names in a combobox

{
  "Message": "[\r\n  {\r\n    \"SteamID\": \"removed\",\r\n    \"OwnerSteamID\": \"0\",\r\n    \"DisplayName\": \"[CS] <Ch@o$> Magician\",\r\n    \"Ping\": 21,\r\n    \"Address\": \"removed\",\r\n    \"ConnectedSeconds\": 13416,\r\n    \"VoiationLevel\": 0.0,\r\n    \"CurrentLevel\": 0.0,\r\n    \"UnspentXp\": 0.0,\r\n    \"Health\": 90.6981354\r\n  },\r\n  {\r\n    \"SteamID\": \"removed\",\r\n    \"OwnerSteamID\": \"0\",\r\n    \"DisplayName\": \"[CS] <Ch@o$> Wolfy\",\r\n    \"Ping\": 36,\r\n    \"Address\": \"remove\",\r\n    \"ConnectedSeconds\": 12310,\r\n    \"VoiationLevel\": 0.0,\r\n    \"CurrentLevel\": 0.0,\r\n    \"UnspentXp\": 0.0,\r\n    \"Health\": 100.0\r\n  },\r\n  {\r\n    \"SteamID\": \"removed\",\r\n    \"OwnerSteamID\": \"0\",\r\n    \"DisplayName\": \"Hellzpower dapubg.com\",\r\n    \"Ping\": 109,\r\n    \"Address\": \"removed\",\r\n    \"ConnectedSeconds\": 8882,\r\n    \"VoiationLevel\": 0.0,\r\n    \"Curr
entLevel\": 0.0,\r\n    \"UnspentXp\": 0.0,\r\n    \"Health\": 80.08678\r\n  }\r\n]",
  "Identifier": 0,
  "Type": "Generic",
  "Stacktrace": ""
}
thomaserlang commented 6 years ago

No worries. But this looks just like the data you posted in the previous issue?

Since Message contains a json encoded string you'll have to parse it after parsing the general text.

procedure test;
var
  data, users, user: TJSON;
begin
  data := TJSON.Parse({JSON TEXT});
  try
    users := TJSON.Parse(data['Message'].AsString);
    try
      for user in users do
      begin
        showmessage(user['SteamID'].AsString);
      end;
    finally
      users.free;
    end;
  finally
    data.free;
  end;
end;
AdriaanBoshoff commented 6 years ago

It is the same data. I just wanted to see how you would parse it because I still keep getting a crash no matter what application I try to parse it with. Thanks for the response.

AdriaanBoshoff commented 6 years ago

I think I know what my issue is. As soon as procedure TForm1.websocketclient1DataIn(Sender: TObject; DataFormat: Integer; Text: string; EOM: Boolean); fires it is still busy receiving data and the parser is trying to parse only have of the data it received and as it's not a valid json then it crashes. wet6nfj

So to test this i removed parsing of data and only let it show on the memo. As the screenshot shows it shows that data in the first second but then adds the rest 3 seconds after. I think I can get around this by putting this in a new thread.

Update:

It seems that I was correct. The top memo shows data from the server thats not running in a thread but the bottom memo shows data that it got from the server while running in a thread. Thus it could be parsed as it got all the data.

project1_2018-04-16_16-43-25

Note to self and others:

Talking to yourself helps a lot.

thomaserlang commented 6 years ago

Ah, that makes sense.