• Delphi XE JSON[3] 解析数据


    {该文首发于博客园 滔Roy,无须授权即可转发,请自觉保留头部申明}

    Delphi XE JSON[3] 解析数据

    1、解析单个文本

    var
      JSONValue: TJSONValue;
      JSObject:TJSONObject;
      JValue:string;
    begin
      if Trim(Memo1.Text)='' then Exit; 
      JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
      JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
    
    
      if JSObject.TryGetValue(EditName.Text,JValue) then
        MemoValue.Text:=JValue;
    
      JSObject.Free;
    end;

            

    2、解析数据(指定数组)

    var
      JSONValue: TJSONValue;
      JSObject:TJSONObject;
      JSONArr:TJSONArray;
    begin
      if Trim(Memo1.Text)='' then Exit;
      JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
      JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
    
      if JSObject.TryGetValue(EditName.Text,JSONArr) then  // JSONArr:=JSObject.GetValue(EditName.Text) as TJSONArray;
      MemoValue.Text:=JSONArr.ToString;
    
      JSObject.Free;
    end;
    

    3、循环解析数据(指定数组)

    var
      JSONValue: TJSONValue;
      JSObject:TJSONObject;
      JSONArr:TJSONArray;
      i:Integer;
      JValue:string;
    begin
      if Trim(Memo1.Text)='' then Exit;
      MemoValue.Clear;
      JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
      JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
    
      if JSObject.TryGetValue(EditName.Text,JSONArr) then  // JSONArr:=JSObject.GetValue(EditName.Text) as TJSONArray;
      for i:=0 to JSONArr.Count-1 do begin
        if JSONArr.Items[i].TryGetValue('名称',JValue) then
        MemoValue.Lines.Add( '名称=' + JValue);
        if JSONArr.Items[i].TryGetValue('数量',JValue) then
        MemoValue.Lines.Add( '数量=' + JValue);
        if JSONArr.Items[i].TryGetValue('价格',JValue) then
        MemoValue.Lines.Add( '价格=' + JValue);
      end;
    
      JSObject.Free;
    
    end;
    

    4、循环格式化(不指定数组)

    var
      JSONValue: TJSONValue;
      JSObject,JSObject1:TJSONObject;
      JSONArr:TJSONArray;
      i,j,k:Integer;
      JName,JName1:string;
      JValue,JValue1:TJSONValue;
    begin
      if Trim(Memo1.Text)='' then Exit;
      MemoValue.Clear;
      JSONValue:=TJSONObject.ParseJSONValue(Memo1.Text);  //使用TJSONObject.ParseJSONValue读取JSON数据
      JSObject:=JSONValue as TJSONObject;      //将JSONValue转换为TJSONObject类型
    
      //第一层
      for i:=0 to JSObject.Count-1 do begin
        JName:=JSObject.Get(i).JsonString.ToString;
        JValue:=JSObject.Get(i).JsonValue;
        MemoValue.Lines.Add(JName+'=');
    
        if JValue is TJSONArray then begin   //先判断类型
          JSONArr:=JSObject.Get(i).JsonValue as TJSONArray;
          for j := 0 to JSONArr.Size-1 do begin
            JSObject1:=JSONArr.Items[j] as TJSONObject;
            for k := 0 to JSObject1.Count-1 do begin
              JName1:=JSObject1.Get(k).JsonString.ToString;
              JValue1:=JSObject1.Get(k).JsonValue;
              MemoValue.Lines.Add('  '+JName1+'='+JValue1.ToString);
            end;
          end;
        end else begin
          MemoValue.Lines.Add(JValue.ToString);
        end;
    
      end;
    
      JSObject.Free;
    
    end;
    

     

      

    创建时间:2022.03.20  更新时间:

  • 相关阅读:
    边走边学Nodejs (基础入门篇)
    Android应用打包安装过程具体解释
    ubuntu与centos安装软件的不同点总结
    你好,C++(12)怎样管理多个类型同样性质同样的数据?3.6 数组
    oracle暂时表空间 ORA-01652:无法通过16(在表空间XXX中)扩展 temp 字段
    iOS中sqlite3操作
    sparkSQL1.1入门之二:sparkSQL执行架构
    [NHibernate]视图处理
    [NHibernate]立即加载
    [NHibernate]延迟加载
  • 原文地址:https://www.cnblogs.com/guorongtao/p/16030179.html
Copyright © 2020-2023  润新知