Einzelnen Beitrag anzeigen

Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#5

AW: Problemme mit DBXJSON

  Alt 12. Jul 2014, 23:25
Hallo,

Zitat:
Und wie liest man einen Boolean-Wert(bei 'dat3') aus? (und null-Wert bei 'dat2')
Vielleicht so:
Delphi-Quellcode:
const
  JSON =
  '{' +
    '"num1":631,' +
    '"num2":31,' +
    '"num3":64,' +
    '"data":"{' +
      '\"dat1\":[0,1,2,3,4],' +
      '\"dat2\":null,' +
      '\"dat3\":true,' +
      '\"dat4\":[1,1,1,1,1],' +
      '\"dat5\":{' +
        '\"dat4str1\":11,' +
        '\"dat4str2\":0,' +
        '\"dat4str3\":0,' +
        '\"dat4str4\":2' +
      '},' +
      '\"sdata\":{' +
        '\"sdata1\":12,' +
        '\"sdata2\":48,' +
        '\"sdata3\":395' +
      '},' +
      '\"dat5\":[[0,1,1],[0,1,1],[0,1,1]]' +
    '}"' +
  '}';

function GetJsonValue (Source : string; const PairNames : array of string) : string;
var
  s : TJSONObject;
  i : Integer;
  p : TJSONPair;
begin
  Result := '';
  if (Trim (Source) = '') or (High (PairNames) = -1) then
    Exit;

  s := TJSONObject.Create;
  try
    p := nil;
    try
      for i := Low (PairNames) to High (PairNames) do
        begin
          s.Parse(BytesOf (Source), 0);

          p := s.Get(PairNames [i]);
          if not Assigned (p) then
            Exit;

          Source := p.JsonValue.Value;
          if Source = 'then
            Source := p.JsonValue.ToString
        end;

      Result := p.JsonValue.ToString
    except
      // Fehlerbehandlung
    end;
  finally
    s.Free
  end
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Label2.Text := GetJsonValue (JSON, ['data', 'dat2'])
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Label3.Text := GetJsonValue (JSON, ['data', 'dat3'])
end;
Vielleicht gibt 's aber auch 'nen besseren Weg

Gruß
Volker Zeller
  Mit Zitat antworten Zitat