Skip to content

Instantly share code, notes, and snippets.

@fabriciocolombo
Last active May 8, 2023 18:30
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fabriciocolombo/4236ce010787d86b5c65 to your computer and use it in GitHub Desktop.
Save fabriciocolombo/4236ce010787d86b5c65 to your computer and use it in GitHub Desktop.
Delphi 2010 Parse Json using DBXJson
uses DBXJSON, Character;
TJsonObjectHelper = class helper for TJsonObject
public
//Helper to find a JSON pair based on the pair string part
function Get(const PairName: UnicodeString): TJSONPair; overload;
end;
{ TJsonObjectHelper }
function TJsonObjectHelper.Get(const PairName: UnicodeString): TJSONPair;
var
Candidate: TJSONPair;
I: Integer;
begin
for i := 0 to Size - 1 do
begin
Candidate := Get(i);
if (Candidate.JsonString.Value = PairName) then
Exit(Candidate);
end;
Result := nil;
end;
//Remove whitespaces http://edn.embarcadero.com/article/40882
function StripNonJson(s: string): string;
var
ch: char;
inString: boolean;
begin
Result := '';
inString := false;
for ch in s do
begin
if ch = '"' then
inString := not inString;
if TCharacter.IsWhiteSpace(ch) and not inString then
continue;
Result := Result + ch;
end;
end;
{$REGION 'Json'}
const
PEDIDOS = '{' +
' "conteudo":{' +
' "pedidos":[' +
' {' +
' "ID":3,' +
' "COD_CLI":"1486",' +
' "DESCONTO":0,' +
' "COD_VEN":999999,' +
' "CONTROLE":0,' +
' "STATUS":1,' +
' "DATA":"03.02.2015",' +
' "OBS":"teste obs",' +
' "FORMA_PAGTO":3,' +
' "VALOR":57.4' +
' },' +
' {' +
' "ID":4,' +
' "COD_CLI":"198",' +
' "DESCONTO":0,' +
' "COD_VEN":12,' +
' "CONTROLE":0,' +
' "STATUS":1,' +
' "DATA":"04.02.2015",' +
' "OBS":"",' +
' "FORMA_PAGTO":6,' +
' "VALOR":100.4' +
' }' +
' ],' +
' "itemPedido":[' +
' {' +
' "COD_PED":3,' +
' "QUANT":50,' +
' "COD_PRO":"736",' +
' "TOTAL":45,' +
' "SEQUENCIA":1,' +
' "DESC":0,' +
' "VRUNI":9' +
' },' +
' {' +
' "COD_PED":3,' +
' "QUANT":50,' +
' "COD_PRO":"736",' +
' "TOTAL":45,' +
' "SEQUENCIA":2,' +
' "DESC":0,' +
' "VRUNI":9' +
' },' +
' {' +
' "COD_PED":4,' +
' "QUANT":1,' +
' "COD_PRO":"897",' +
' "TOTAL":10,' +
' "SEQUENCIA":1,' +
' "DESC":0,' +
' "VRUNI":10' +
' }' +
' ]' +
' }' +
'}';
{$ENDREGION}
procedure TForm1.ReadJson(const AJsonText: string);
var
vJson: TJSONObject;
vConteudo: TJSONObject;
vPedidos, vItemPedido: TJSONArray;
vPedido, vItem: TJSONObject;
i: Integer;
j: Integer;
begin
vJson := TJsonObject(TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StripNonJson(AJsonText)),0));
try
vConteudo := TJSONObject(vJson.Get('conteudo').JsonValue);
vPedidos := TJSONArray(vConteudo.Get('pedidos').JsonValue);
vItemPedido := TJSONArray(vConteudo.Get('itemPedido').JsonValue);
Memo1.Lines.Add('PEDIDOS: ');
for i := 0 to vPedidos.Size-1 do
begin
vPedido := TJsonObject(vPedidos.Get(i));
for j := 0 to vPedido.Size-1 do
begin
Memo1.Lines.Add(' ' + vPedido.Get(j).JsonString.Value + ':' + vPedido.Get(j).JsonValue.Value);
end;
end;
Memo1.Lines.Add('ITENS: ');
for i := 0 to vItemPedido.Size-1 do
begin
vItem := TJsonObject(vItemPedido.Get(i));
for j := 0 to vItem.Size-1 do
begin
Memo1.Lines.Add(' ' + vItem.Get(j).JsonString.Value + ':' + vItem.Get(j).JsonValue.Value);
end;
end;
finally
vJson.Free;
end;
end;
@omidrahimian
Copy link

OK, ok, ok
very very thanks

@thiago-santanna
Copy link

Muito Massa, ficou muito mais amigável trabalhar com o nome, Muito Obrigado Caro amigo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment