当前位置: 移动技术网 > IT编程>开发语言>Delphi > Delphi解析修改Json文件,基于superobject.pas(ISuperObject)

Delphi解析修改Json文件,基于superobject.pas(ISuperObject)

2019年05月09日  | 移动技术网IT编程  | 我要评论

在经过一系列的波折后,还是觉得delphi读取并修改json文件来的方便;

在网络上找到一个delphi的三方库isuperobject,添加到项目后直接引用就行;

isuperobject中几个常用的函数:

  • function so(const s: sostring = ‘{}’): isuperobject; overload; 此函数传入json数据字符串,并返回一个isuperobject对象,这一般是我们解析json时使用的第一个函数,如jobj := so(jsonstr)。
  • property o[const path: sostring]: isuperobject read geto write puto; default; 如:jobj.o[‘username’],此函数被一个isuperobject对象调用,方括号内的字符串为json中的字段名称,返回一个isuperobject对象。
  • property s[const path: sostring]: sostring read gets write puts; 此函数被一个isuperobject对象调用,和o[‘username’]不同的是,它返回的是一个sostring,即一个字符串,使用方法 str := jobj.s[‘username’]; 同理的还有其他几个类似的函数,如i[‘age’]返回整数,b[‘isenable’]返回布尔型,a[‘users’]返回一个tsuperarray数组
  • asstring, asboolean, asinteger,asarray,isuperobject的函数,用来把isuperobject转换成相应的数据类型。

演示代码:

 

//读取json文件
procedure tform2.button1click(sender: tobject);
var
  rtext: textfile;
  tmp: utf8string;
begin
  assignfile(rtext, 'c:\users\admin\desktop\f.txt');
  reset(rtext);
  while not eof(rtext) do
  begin
    readln(rtext, tmp);
    res := res + tmp;
  end;
  closefile(rtext);
  memo1.lines.add(res);
  application.messagebox('加载成功!', '提示', mb_ok);
end;
//处理json字符串,按照固定结构读取
procedure tform2.button2click(sender: tobject);
var
  jret, jusers: isuperobject;
  aryusers: tsuperarray;
  gridstarttime, gridendtime, resultstarttime, resultendtime, windspeedborder,
    winddirectborder: string;
  po, pof, poh, posp: tpoint;
  num, count, i, j: integer;
  aryers, arrline, arrbar, arrspeed: tsuperarray;
  tlist: tstringlist;
begin
  jret:=tsuperobject.create(stobject);
  tlist := tstringlist.create;
  tlist.delimiter := ',';
  jret := so(res);
  gridstarttime := jret.o['gridstarttime'].asstring();
  memo1.lines.add(gridstarttime);
  gridendtime := jret.o['gridendtime'].asstring();
  memo1.lines.add(gridendtime);
  resultstarttime := jret.o['resultstarttime'].asstring();
  memo1.lines.add(resultstarttime);
  resultendtime := jret.o['resultendtime'].asstring();
  memo1.lines.add(resultendtime);
  windspeedborder := jret.o['windspeedborder'].asstring();
  memo1.lines.add(windspeedborder);
  winddirectborder := jret.o['winddirectborder'].asstring();
  memo1.lines.add(winddirectborder);

  aryers := jret.o['winddirectline'].o['coordinates'].asarray();
  count := aryers.length;
  for i := 0 to count - 1 do
  begin
    arrline := aryers[i].o['mwinddirectbaseline'].asarray();
    for j := 0 to arrline.length - 1 do
    begin
      // tlist.delimitedtext := arrline[j].o['p'].asstring();
      // po:=point(strtoint(tlist[0]),strtoint(tlist[1]));
      memo1.lines.add(arrline[j].o['p'].asstring());
    end;
    arrbar := aryers[i].o['mwinddirectbars'].asarray();
    for j := 0 to arrbar.length - 1 do
    begin
      // tlist.delimitedtext := arrbar[j].o['f'].o['p'].asstring();
      // pof:=point(strtoint(tlist[0]),strtoint(tlist[1]));
      // tlist.delimitedtext := arrbar[j].o['h'].o['p'].asstring();
      // poh:=point(strtoint(tlist[0]),strtoint(tlist[1]));
      memo1.lines.add('f: ' + arrbar[j].o['f'].o['p'].asstring()
          + ' h: ' + arrbar[j].o['h'].o['p'].asstring());
    end;
  end;

  aryers := jret.o['windspeedline'].o['coordinates'].asarray();
  count := aryers.length;
  for i := 0 to count - 1 do
  begin
    arrspeed := aryers[i].asarray();
    for j := 0 to arrspeed.length - 1 do
    begin
      // tlist.delimitedtext := arrspeed[j].o['p'].asstring();
      // posp:=point(strtoint(tlist[0]),strtoint(tlist[1]));
      memo1.lines.add(arrspeed[j].o['p'].asstring());
    end;
  end;
  application.messagebox('处理完成!', '提示', mb_ok);
end;
//修改json文件并保存
procedure tform2.button3click(sender: tobject);
var
  jfields, jitems, jo, jret: isuperobject;
  date: string;
  num, count, i, j: integer;
  txt:textfile;
  aryers, arrline, arrbar, arrspeed: tsuperarray;
begin
  jret := so(res);
  if jret = nil then
       application.messagebox('读取失败!', '提示', mb_ok);
//  date := formatdatetime('yyyy-mm-dd hh:nn:ss', now());
  jret.s['gridstarttime'] := date;
  jret.s['gridendtime'] := date;
  jret.s['resultstarttime'] := date;
  jret.s['resultendtime'] := date;
  jret.s['windspeedborder'] := date;
  jret.s['winddirectborder'] := date;
  jret.i['gridmethod']:=2;
  count:=jret.o['winddirectline'].a['coordinates'].length;
  for i := 0 to count - 1 do
  begin
      num:= jret.o['winddirectline'].a['coordinates'][i].a['mwinddirectbaseline'].length;
      for j := 0 to num - 1 do
      begin
        jret.o['winddirectline'].a['coordinates'][i].a['mwinddirectbaseline'][j].s['p']:=inttostr(i)+' ,'+inttostr(j);
      end;
  end;
  assignfile(txt,'e:\p.gal');  //指定文件路径
  rewrite(txt);               //创建并打开一个新文件(或覆盖原有文件)
  writeln(txt,jret.asstring());
  closefile(txt);            //关闭打开的文件

  application.messagebox(pchar(jret.o['gridstarttime'].asstring()), '提示', mb_ok);
end;

注意:

  • 读取文件时如果json文件中包含中文格式,那么在读取时必须保证为获取字段为utf8string类型。
  •  jret.o['windspeedline'].a['coordinates'][0].asarray().add(jo);//对与数组嵌套的json,可以通过这种方式添加。
  • 读取或者声明的jo := so();不能用clear()清空,否则add(jo)就会成为空数据。

  

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网