zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    关于MySQL数据库优化的部分整理
    PHP跨域form提交
    px、dp和sp,这些单位有什么区别?
    301和302 Http状态有啥区别?
    PHP HTTP请求
    php的http_build_query使用
    nginx ssi 模块
    MongoDB学习笔记(一) MongoDB介绍及安装(摘)
    Django Admin 录入中文错误解决办法
    关于python字符串连接的操作
  • 原文地址:https://www.cnblogs.com/Thenext/p/10288547.html
Copyright © 2011-2022 走看看