【问题标题】:How to parse a JSON string in Delphi?如何在 Delphi 中解析 JSON 字符串?
【发布时间】:2011-05-20 01:12:06
【问题描述】:

如何解析 JSON 字符串

{"data":{"results":[{"Branch":"ACCT590003"}]}}

使用TJSONObject 对象?我想从此字符串中获取ACCT590003 值。

【问题讨论】:

    标签: delphi json delphi-xe


    【解决方案1】:
    uses
      SysUtils,
      DBXJSON;
    
    type
      TProcessJSONString = TProc<TJSONString>;
    
    procedure DoJSONObject(o: TJSONObject; Process: TProcessJSONString); forward;
    
    procedure DoJSONArray(o: TJSONArray; Process: TProcessJSONString);
    var i: integer;
        v: TJSONValue;
    begin
      for i := 0 to o.Size - 1 do begin
        v := o.Get(i);
        if v is TJSONObject then
          DoJSONObject(v as TJSONObject, Process);
      end;
    end;
    
    procedure DoJSONObject(o: TJSONObject; Process: TProcessJSONString);
    var i: integer;
        p: TJSONPair;
    begin
      for i := 0 to o.Size - 1 do begin
        p := o.Get(i);
        Process(p.JsonString);
        if p.JsonValue is TJSONObject then
          DoJSONObject(p.JsonValue as TJSONObject, Process)
        else if p.JsonValue is TJSONArray then
          DoJSONArray(p.JsonValue as TJSONArray, Process)
        else if p.JsonValue is TJSONString then
          Process(p.JsonValue as TJSONString);
      end;
    end;
    
    var o: TJSONObject;
    begin
      o := TJSONObject.ParseJSONValue('{"data":{"results":[{"Branch":"ACCT590003"}]}}') as TJSONObject;
      try
        DoJSONObject(o,
          procedure (o: TJSONString)
          begin
            WriteLn(o.ToString);
          end
        );
      finally
        o.Free;
      end;
      ReadLn;
    end.
    

    【讨论】:

    • 虽然不错,但通常更倾向于使用系统可用的库来更轻松地维护代码。
    • 这正是我所需要的。拯救了我的一天!
    【解决方案2】:

    使用TALdocument 很简单

    AJsonDoc := TalJsonDocument.create;
    AjsonDoc.loadFromJsonString('{"data":{"results":[{"Branch":"ACCT590003"}]}}');
    writeln(AjsonDoc.childnode['data']['result'][0]['Branch'].text);
    

    【讨论】:

      【解决方案3】:

      使用超级对象库https://github.com/hgourvest/superobject/

      var json: iSuperObject;
          data: string;
      
      begin
        json := SO('{"data":{"results":[{"Branch":"ACCT590003"}]}}'); // shorthand
      // or equal:  JSON := TSuperObject.ParseString('{"data":{"results":[{"Branch":"ACCT590003"}]}}');
      
        data := json.S['data.results[0].Branch'];
      
        WriteLn('Result is: ', data);
      end.
      

      【讨论】:

        【解决方案4】:

        试试这个代码,效果很好

        uses System.JSON;
        
        procedure _Parse_JSonValue;
        var
           JSonObject:TJSonObject;
           JSonValue:TJSonValue;
           st:string;
           Branch: string;
        Begin
           st := '{"data":{"results":[{"Branch":"ACCT590003"}]}}';
           JSonObject := TJSonObject.Create;
           JsonValue:=JSonObject.ParseJSONValue(st);
           JsonValue:=(JsonValue as TJSONObject).Get('data').JSONValue;
           JsonValue:=(JsonValue as TJSONObject).Get('results').JSONValue;
           if (JSONValue is TJSONArray) then
              Branch := ((JSONValue as TJSONArray).Items[0] as TJSonObject).Get('Branch').JSONValue.Value;
           JSonObject.Free;
        End;
        

        分支 = 'ACCT590003'

        【讨论】:

          【解决方案5】:

          您不需要使用外部库来执行 JSONPath 搜索。 以 Delphi 10 Seattle 为例:

          uses  System.JSON;
          procedure ParseJSonValue;
          var
             JSonValue:TJSonValue;
             st:string;
             Branch: string;
          begin
             st := '{"data":{"results":[{"Branch":"ACCT590003"}]}}';
             JsonValue := TJSonObject.ParseJSONValue(st);
             Branch := JsonValue.GetValue<string>('data.results[0].Branch');
             JsonValue.Free;
          end;
          

          【讨论】:

          • 最初的问题是关于创建TJSonObject 的实例,而不是关于接收TJsonValue.. ^^
          【解决方案6】:

          用 Delphi 认识 Json 的最佳视频 Learn How to Use the New JSON Features in RAD Studio 10 Seattle

          这里是我的示例。

          procedure TForm1.BitBtn2Click(Sender: TObject);
          Var
             W : TStringWriter;
             JW : TJsonTextWriter;
            I: Integer;
          begin
             W :=TStringWriter.Create;
             JW := TJsonTextWriter.Create(W);
             With JW Do
                Begin
                   Formatting:=TJsonFormatting.Indented;
                   WriteStartObject;
                   WritePropertyName('CheckListBox1');
                   WriteStartArray;
                   for I := 0 to CheckListBox1.Items.Count-1  do
                       Begin
                         WriteStartObject;
                         WritePropertyName(CheckListBox1.Items[I]);
                         WriteValue(CheckListBox1.Checked[I]);
                         WriteEndObject;
                       End;
                    WriteEndArray;
                    WriteEndObject;
                End;
              Memo1.Clear;
              Memo1.Lines.Text:=W.ToString
          
          end;
          

          别忘了使用这个单位

          System.JSON.Writers,System.JSON.readers,System.JSON,System.JSON.Types
          

          用于读取复选框

          procedure TForm1.BitBtn3Click(Sender: TObject);
          Var
             W : TStringReader;
             JW : TJsonTextReader;
            I: Integer;
          begin
             W :=TStringReader.Create(Memo1.Lines.Text);
             JW := TJsonTextReader.Create(W);
             I:=0;
             while JW.Read do
                 Begin
                    if (JW.Depth = 3) Then
                       Begin
                          CheckListBox2.Items.Add(JW.Value.ToString);
                          JW.Read;
                          CheckListBox2.Checked[I]:=JW.Value.AsBoolean;
                          Inc(I);
                       End;
                 End;
          
          end;
          

          【讨论】:

          • 你真的读过这个问题吗?您应该删除您的答案以避免失去声誉。
          • 为什么?我寻找答案并搜索并看到这个问题。这个网站上的回答都没有帮助我......所以我试图为下一个访问者保存这个答案。声誉对我来说并不重要
          • 这里的问题(见页面顶部)是关于从 JSON 格式的字符串中提取数据的。你的回答根本没有做到这一点。您的答案是关于创建 JSON 格式的字符串,这与所要求的相反。你不会帮助任何回答不好的人。如果您正在寻找自己的东西但没有找到,请打开一个新问题。
          • 为您的时间和您的 cmets 加油
          猜你喜欢
          • 2015-10-02
          • 2016-12-05
          • 2014-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-10
          • 1970-01-01
          相关资源
          最近更新 更多