• Delphi 10.3中使用JSON


    有一个对JSON处理的单元,在你需要使用JSON的单元里面引入"System.json",随后你就可以用Delphi自己的json处理类了。

    注意:
      1,JSON类创建后,里面所有元素不用管释放,JSON类自己管理,千万不要画蛇添足啊!!!!!!
     
    const
      // 演示用的JSON
      jsonString = '{"name":"张三", "other":["中国","程序员"]}';
     
    const
      // 演示用的JSON
      jsonString = '{"name":"张三", "other":["中国","程序员"]}';
    
    
    implementation
    
    
    {$R *.fmx}
    
    
    uses
      System.json; // Dephi自带的JSON单元
    
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      JSONObject: TJSONObject; // JSON类
      i: Integer; // 循环变量
      temp: string; // 临时使用变量
      jsonArray: TJSONArray; // JSON数组变量
    begin
    
    
      if Trim(Memo1.Text) = '' then
      begin
        ShowMessage('要解析数据不能为空!');
      end
      else
      begin
        JSONObject := nil;
        try
          { 从字符串生成JSON }
          JSONObject := TJSONObject.ParseJSONValue(Trim(Memo1.Text)) as TJSONObject;
    
    
          if JSONObject.Count > 0 then
          begin
    
    
            { 1,遍历JSON数据 }
            Memo2.Lines.Add('遍历JSON数据:' + #13#10);
    
    
            Memo2.Lines.Add('JSON数据数量:' + IntToStr(JSONObject.Count));
    
    
            for i := 0 to JSONObject.Count - 1 do
            begin
    
    
              if i = 0 then
              begin
                temp := JSONObject.Get(i).ToString + #13#10;;
              end
              else
              begin
                temp := temp + JSONObject.Get(i).ToString + #13#10;
              end;
    
    
            end;
    
    
            { output the JSON to console as String }
            Memo2.Lines.Add(temp);
    
    
            Memo2.Lines.Add('------------------------------');
    
    
            { 2,按元素解析JSON数据 }
            Memo2.Lines.Add('按元素解析JSON数据:' + #13#10);
            temp := 'name = ' + JSONObject.Values['name'].ToString + #13#10;
            Memo2.Lines.Add(temp);
    
    
            // json数组
            jsonArray := TJSONArray(JSONObject.GetValue('other'));;
            if jsonArray.Count > 0 then
            begin
    
    
              // 得到JSON数组字符串
              temp := 'other = ' + JSONObject.GetValue('other').ToString + #13#10;
    
    
              // 循环取得JSON数组中每个元素
              for i := 0 to jsonArray.Size - 1 do
              begin
                temp := temp + IntToStr(i + 1) + ' : ' + jsonArray.Items[i]
                  .Value + #13#10;
              end;
    
    
            end;
    
    
            Memo2.Lines.Add(temp);
    
    
          end
          else
          begin
            temp := '没有数据!';
            Memo2.Lines.Add(temp);
          end;
    
    
        finally
          JSONObject.Free;
        end;
      end;
    
    
    end;
  • 相关阅读:
    C# MVC跳转
    从字符串中提取数字
    使用Node.js+Socket.IO搭建WebSocket实时应用
    C# 计算当前时间距离今晚00:00:00还有多少分多少秒
    C#错误异常列表
    HTTP请求报文和HTTP响应报文
    Selenium2(webdirver)入门之环境搭建(Java版)
    mysql grant ,User,revoke
    mysql 用drop和delete方法删除用户的区别
    [MySQL]
  • 原文地址:https://www.cnblogs.com/Thenext/p/10288547.html
Copyright © 2020-2023  润新知