Created
April 19, 2016 18:50
-
-
Save adilsoncarvalho/94e7d8bafe3821199993b2dda691beb3 to your computer and use it in GitHub Desktop.
Parsing JSON on Delphi (the ugly way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit cl_ReadJSON; | |
interface | |
uses System.Json, System.SysUtils, Vcl.Dialogs, System.Classes; | |
procedure ReadJSON; | |
implementation | |
const StrJson = | |
'[{"channel":"/meta/handshake","successful":true,"version":"1.0","supportedConnectionTypes":["long-polling",'+ | |
'"cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"],"clientId":"pl4t3hchpz8t6jb0ojnzd2cccskde5r","advice":{"reconnect":"retry","interval":0,"timeout":45000}}]'; | |
procedure ReadJSON; | |
var LJSONBytes: TBytes; | |
LJSONSObj: TJSONValue; | |
LJSONArray: TJSONArray; | |
LJSONValue, LReceive: TJSONValue; | |
LJPair: TJSONPair; | |
size: integer; | |
i: Integer; | |
ii: Integer; | |
Tamanho: Integer; | |
begin | |
LJSONBytes := TEncoding.ASCII.GetBytes(StrJson); | |
LJSONSObj := TJSONObject.ParseJSONValue(LJSONBytes, 0); | |
if LJSONSObj <> nil then | |
try | |
LJSONArray := LJSONSObj as TJSONArray; | |
Size := TJSONArray(LJSONArray).Count; | |
for i := 0 to Pred(Size) do | |
begin | |
ShowMessage( TJSONObject(TJSONArray(LJSONArray).Items[i]).Get('channel').JsonValue.ToString); | |
ShowMessage( TJSONObject(TJSONArray(LJSONArray).Items[i]).Get('successful').JsonValue.ToString); | |
ShowMessage( TJSONObject(TJSONArray(LJSONArray).Items[i]).Get('version').JsonValue.ToString); | |
ShowMessage('Conteudo do Array supportedConnectionTypes'); | |
LJSONValue := TJSONObject(TJSONArray(LJSONArray).Items[i]).Get('supportedConnectionTypes').JsonValue; | |
LJPair := TJSONPair(LJSONValue); | |
Tamanho := TJSONArray(LJPair).Count; | |
//loop Leitura do array supportedConnectionTypes | |
for ii := 0 to Pred(Tamanho) do | |
begin | |
LReceive := TJSONArray(LJPair).Items[ii]; | |
ShowMessage(LReceive.ToString); | |
end; | |
ShowMessage( TJSONObject(TJSONArray(LJSONArray).Items[i]).Get('clientId').JsonValue.ToString); | |
ShowMessage('Conteudo do Objeto advice'); | |
LJSONValue := TJSONObject(TJSONArray(LJSONArray).Items[i]).Get('advice').JsonValue; | |
SHowMessage( TJSONObject(TJSONArray(LJSONValue)).Get('reconnect').JsonValue.ToString); | |
SHowMessage( TJSONObject(TJSONArray(LJSONValue)).Get('interval').JsonValue.ToString); | |
SHowMessage( TJSONObject(TJSONArray(LJSONValue)).Get('timeout').JsonValue.ToString); | |
end; | |
finally | |
LJSONSObj.Free; | |
end; | |
end; | |
end. |
You are welcome.
Muchas gracias, funciona!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.