mcjson_根据InPath路径,查询mcjson中的值 如果该路径不存在,则创建该路径并赐予‘‘空值

go 复制代码
//根据InPath路径,查询mcjson中的值 如果该路径不存在,则创建该路径并赐予''空值
// InJson  json数据
//查询json路径格式 InPath:='601988\策略名1\日期5\参数2';
function TForm1.GegMcJsonValue(var InJson:TMcJsonItem;InPath:string): Variant;
var
//     InJson: TMcJsonItem;
  List1:TStringList;
   JSPath:string;
   str1,str2:string;
   ifor1:Integer;
 begin
//      InJson := TMcJsonItem.Create;
  Result :='';
   if InJson.Path(InPath)<>nil then  begin
     Result :=InJson.Path(InPath).Value;
     exit;
   end;
  try
    List1:=TStringList.Create;
  //     InJson.AsJSON := '{"601988":{"策略名1": {"日期1":{"参数1":1,"参数2":1}}}}';
    str1:=InPath;//'601988\策略名1\日期5\参数2';
    List1.Delimiter := '\';
    List1.DelimitedText :=str1;
    str1:=List1[0];
     for ifor1:=0 to List1.Count -1 do begin
       if (ifor1=0) and (InJson.Path(str1)=nil) then
       begin
         InJson.add(List1[0]).AsJSON:='';
         continue;
       end;
      if ifor1>0  then begin
         str2:=str1;
         str1:=Format('%s\%s',[str1,List1[ifor1]]);
         if InJson.Path(str1)=nil then
         begin
           InJson.Path(str2).Add(List1[ifor1]).AsJSON:='';
         end;
       end;
     end;
   finally
    List1.Free;
  end;
end;

调用方法

go 复制代码
procedure TForm1.btn5Click(Sender: TObject);
var
  N: TMcJsonItem;
  str1:string;
  outvar:Variant;
begin

  N := TMcJsonItem.Create();
 N.AsJSON:='{"601988":{"策略名1": {"日期1":{"参数1":1,"参数2":888}}}}';
  str1:='601988\策略名1\日期5\参数2';
  GegJsonValue(N,str1);
  str1:='601988\策略名1\日期5\参数1';
  GegJsonValue(N,str1);
   str1:='601988\策略名1\日期1\参数2';
 outvar:= GegJsonValue(N,str1)+2;
  mmo2.Text:=N.AsJSON;
  mmo1.Lines.Add(outvar);
  n.Free;
end;

//<<==========================================

//循环读取key

go 复制代码
//循环读取key
procedure TForm1.btn6Click(Sender: TObject);
var
  N,m: TMcJsonItem;
  str1,str2,str3:string;
  ifor1,i1:Integer;
begin 
    N := TMcJsonItem.Create;
    m := TMcJsonItem.Create;
    try
      str1:='{"A1":{"B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}},"B2":{"C1":{"D1":5,"D2":6},"C2":{"D1":7,"D2":8}},"B3":{"C1":{"D1":9,"D2":10},"C2":{"D1":11,"D2":12}}}}';
      mmo1.Text:=str1;
      N.AsJSON:=str1;
      str2:='A1';
      i1:=  n.Path('A1').Count;
      mmo1.Lines.Add(Format('A1下有%d条数据',[i1]));
        for ifor1:=0 to i1-1 do begin
         str3 := n.Path('A1').Keys[ifor1];  //获取key的名称
         mmo1.Lines.Add(Format('A1下的Keys%d = %s ',[ifor1,str3]) );
 
          str1 :=Format('%S.%S',[str2,str3]);
           mmo1.Lines.Add(str1);
          if N.Path(str1) <> nil then  begin
            try
            str1 := N.Path(str1).AsString;
            mmo1.Lines.Add(Format('找到指定%s值 = %s ',[str2,str1]) ) ;
            except
            str1 := N.Path(str1).AsJSON;
            mmo1.Lines.Add(Format('找到相关%s的JSON = %s ',[str2,str1]) ) ;
            end;
          end; 
        end;
    finally
     N.Free;
    end;
end;
javascript 复制代码
返回值
{"A1":{"B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}},"B2":{"C1":{"D1":5,"D2":6},"C2":{"D1":7,"D2":8}},"B3":{"C1":{"D1":9,"D2":10},"C2":{"D1":11,"D2":12}}}}
A1下有3条数据
A1下的Keys0 = B1 
A1.B1
找到相关A1的JSON = "B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}} 
A1下的Keys1 = B2 
A1.B2
找到相关A1的JSON = "B2":{"C1":{"D1":5,"D2":6},"C2":{"D1":7,"D2":8}} 
A1下的Keys2 = B3 
A1.B3
找到相关A1的JSON = "B3":{"C1":{"D1":9,"D2":10},"C2":{"D1":11,"D2":12}} 

//>>==========================================

//<<==========================================

//读取json的值,路径必需指定到最终位置

go 复制代码
//读取json的值,路径必需指定到最终位置
procedure TForm1.btn7Click(Sender: TObject);
var
  N: TMcJsonItem;
  str1:string;
begin
  str1:='{"A1":{"B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}},"B2":{"C1":{"D1":5,"D2":6},"C2":{"D1":7,"D2":8}}}}';
  mmo1.Text:=str1;
  N := TMcJsonItem.Create;
  try
    N.AsJSON :=str1;
    str1 :=N.Path('A1.B1.C2.D2').AsString;//  str1 :=N['a']['B1'].AT(1).AsString;    str1 :=N['a']['B1']['k2'].AsString;
    mmo1.Lines.Add(Format('A1.B1.C2.D2 = %s ',[str1]) );
    str1 :=N.Path('A1.B1.C2.D2').AsString;
    mmo1.Lines.Add(Format('A1.B1.C2.D2= %s ',[str1]) );
    str1 :=N.Path('A1').Items[1].Items[1].Items[0].AsString; //str1 :=N.Path('a').AT(1).AT(0).AsString;//  str1 :=N['a']['B1'].AT(1).AsString;    str1 :=N['a']['B1']['k2'].AsString;
    mmo1.Lines.Add(Format('A1.B2.C2.D1= %s ',[str1]));
    str1 :=N.AT(0).AT(1).AT(1).AT(0).AsString; //str1 :=N.Path('a').AT(1).AT(0).AsString;//  str1 :=N['a']['B1'].AT(1).AsString;    str1 :=N['a']['B1']['k2'].AsString;
    mmo1.Lines.Add(Format('A1.B2.C2.D1= %s ',[str1]) );
  finally
   N.Free;
  end;
  
end;
javascript 复制代码
返回值
{"A1":{"B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}},"B2":{"C1":{"D1":5,"D2":6},"C2":{"D1":7,"D2":8}}}}
A1.B1.C2.D2 = 4 
A1.B1.C2.D2= 4 
A1.B2.C2.D1= 7 
A1.B2.C2.D1= 7 

//>>==========================================

//<<==========================================

//找到指定路径值,找到相关路径s的JSON

go 复制代码
//找到指定路径值,找到相关路径s的JSON
procedure TForm1.btn8Click(Sender: TObject);
var
  N: TMcJsonItem;
  str1,str2:string;
begin
  str1:='{"A1":{"B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}},"B2":{"C1":{"D1":5,"D2":6},"C2":{"D1":7,"D2":8}}}}';
  mmo1.Text:=str1;
  N := TMcJsonItem.Create;
  try
    N.AsJSON :=str1;
      str1 :='A1.B1';
      if N.Path(str1) = nil then
       mmo1.Lines.Add(Format('N = %s ',[str1]) ) ELSE  mmo1.Lines.Add(Format('Y = %s ',[str1]) );

      //<<找到指定值
      str1 :='A1.B1.C2.D2';
      str2:=str1;
     if N.Path(str1) <> nil then  begin
       try
         str1 := N.Path(str1).AsString;
         mmo1.Lines.Add(Format('找到指定%s值 = %s ',[str2,str1]) ) ;
       except
         str1 := N.Path(str1).AsJSON;
         mmo1.Lines.Add(Format('找到相关%s的JSON = %s ',[str2,str1]) ) ;
       end;
     end;
    //>>

     //<<找到相关的JSON
       str1 :='A1.B1'; str2:=str1;
     if N.Path(str1) <> nil then  begin
      try
         str1 := N.Path(str1).AsString;
         mmo1.Lines.Add(Format('找到指定%s值 = %s ',[str2,str1]) ) ;
       except
         str1 := N.Path(str1).AsJSON;
         mmo1.Lines.Add(Format('找到相关%s的JSON = %s ',[str2,str1]) ) ;
       end;
     end;
     //>>
  finally
     N.Free;
  end;    
end;
javascript 复制代码
返回值
{"A1":{"B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}},"B2":{"C1":{"D1":5,"D2":6},"C2":{"D1":7,"D2":8}}}}
Y = A1.B1 
找到指定A1.B1.C2.D2值 = 4 
找到相关A1.B1的JSON = "B1":{"C1":{"D1":1,"D2":2},"C2":{"D1":3,"D2":4}} 

//>>==========================================

相关推荐
ufo20061 个月前
fastreport导出PDF后style bold粗体斜体等字体风格不显示的原因
pdf·delphi·fastreport
pcplayer1 个月前
WEB 编程:使用富文本编辑器 Quill 配合 WebBroker 后端
前端·后端·delphi·web开发·webbroker
aroc_lo2 个月前
DELPHI编译软件时带上当前IDE的版本号
ide·delphi
sensor_WU2 个月前
【Delphi】Delphi 中的 LiveBindings 使用场景与概念
delphi·livebindings
c4fx2 个月前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
c4fx2 个月前
Delphi5实现主要——明细型数据库应用
数据库·sqlserver·delphi
new_BCBER3 个月前
修改TMS IWAdvWebGrid日期格式
服务器·前端·javascript·delphi
winfredzhang3 个月前
动态创建 Delphi 按钮的完整指南:基于配置文件的 `TGridPanel` 实现
delphi·配置文件·动态·gridpanel
c4fx3 个月前
Delphi5+SqlServer实现会员数据增删改查
数据库·sqlserver·delphi