Delphi使用JSON

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写,同时也易于机器解析和生成。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。

       JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写,同时也易于机器解析和生成。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。

       Delphi7中不支持JSON文件的解析,可以下载LkJSON(一个JSON的解析文件),然后进行解析。例如

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1
= class(TForm)
Button1: TButton;
memo1: TMemo;
Memo2: TMemo;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses uLkJSON,PerlRegEx;

{$R *.dfm}

procedure listarray(returnval: string; memo: Tmemo);
var
json, item: TlkJSONbase;
i: integer;
list: TlkJSONObject;
str: TlkJSONstring;
begin
json :
= TlkJSON.ParseText(returnval);
for i := 0 to pred(json.Count) do
begin
item :
= TlkJSONObject(json).child[i];
// This is the named array, name in name, the array in Objvalue
Memo.Lines.Add(TlkJSONobjectmethod(item).name);
// list:=TlkJSONobject(TlkJSONobjectmethod(item).Objvalue);
end;
end;

procedure getlist(returnval: string; memo: Tmemo);
var
Jstart: TlkJSONbase;
jAddr: TlkJSONList;
s:
string;
begin
Jstart :
= TlkJSON.ParseText(returnval) as TlkJSONobject;
//Jstart.field 为jbase时,
s :
= vartostr(Jstart.Field['mac'].Field['lv'].Value);
memo.Lines.add(s);
//Jstart.field 有子数据为jslist时
s :
= vartostr(Jstart.Field['ranch'].Field['ranchs'].Child[1].field['hp'].Value);
memo.Lines.add(s);
end;

procedure getlist1(returnval: string; memo: Tmemo);
var
Jstart: TlkJSONbase;
jAddr: TlkJSONList;
s:
string;
begin
Jstart :
= TlkJSON.ParseText(returnval) as TlkJSONobject;
//Jstart.field 为jbase时,
s :
= vartostr(Jstart.Field['list'].Field['key'].Value);
memo.Lines.add(s);
//Jstart.field 有子数据为jslist时
s :
= vartostr(Jstart.Field['list'].Field['script'].field['main'].Field['key'].Value);
memo.Lines.add(s);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
vJsonObj: TlkJsonObject;
vJsonStr:
string;
begin
memo1.Lines.Clear;
Memo2.Lines.Clear;
vJsonObj :
= TlkJSONstreamed.loadfromfile('d:\json2.txt') as TlkJsonObject;
vJsonStr :
= TlkJSON.GenerateText(vJsonObj);
memo1.Text :
= vjsonstr;
listarray(memo1.Text, memo2);
Memo2.Lines.Add(
'');
getlist(memo1.Text, memo2);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
vJsonObj: TlkJsonObject;
vJsonStr:
string;
begin
memo1.Lines.Clear;
Memo2.Lines.Clear;
vJsonObj :
= TlkJSONstreamed.loadfromfile('d:\json.txt') as TlkJsonObject;
vJsonStr :
= TlkJSON.GenerateText(vJsonObj);
memo1.Text :
= vjsonstr;
listarray(memo1.Text, memo2);
Memo2.Lines.Add(
'');
getlist1(memo1.Text, memo2);
end;
end.

其中json.txt文件内容为:

{
 "ranch":
 {"lv":"10","fg":"433","fd":"440","j":"2","ranchs":
  [{"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"0","stat":"0","item":"1014","fif":"0"},
   {"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"1","stat":"0","item":"1014","fif":"0"},
   {"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"2","stat":"0","item":"1014","fif":"0"},
   {"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"3","stat":"0","item":"1014","fif":"0"},
   {"tti":46800,"ti":5053,"we":"0","wo":"0","ar":"4","ph":1,"num":57,"or":57,"hp":"100","se":"3","pt":38,"st":0,"id":"4","stat":"0","item":"1014","fif":"1"},
   {"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"5","stat":"0","item":"1014","fif":"0"},
   {"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"6","stat":"0","item":"1014","fif":"0"},
   {"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"7","stat":"0","item":"1014","fif":"0"},
   {"tti":0,"ti":0,"we":"0","wo":"0","ar":"0","ph":6,"num":56,"or":57,"hp":"100","se":"2","pt":38,"st":0,"id":"8","stat":"0","item":"1014","fif":"0"},
   {"tti":46800,"ti":5048,"we":"0","wo":"0","ar":"4","ph":1,"num":57,"or":57,"hp":"99","se":"3","pt":38,"st":0,"id":"9","stat":"0","item":"1014","fif":"1"},
   {"tti":46800,"ti":5050,"we":"0","wo":"0","ar":"4","ph":1,"num":57,"or":57,"hp":"96","se":"3","pt":38,"st":0,"id":"10","stat":"0","item":"1014","fif":"1"},
   {"id":"11","stat":"0","item":"0","fif":"0"}]
 },
 "de":[],
 "pi":
 {"id":"1054966","uid":"26905522","n":"pan","lv":"26","exp":15886,"gg":"620564","db":"50","gold":0,"etn":21870,"sc":"156","gc":"195",
   "ac":
   {"sp":4370,"id":12}},"gc":"0","task":{"tid":"0","ti":"5","ht":0},"nc":1,"df":0,"mac":{"lv":"12","exp":989,"en":"133.00"},
    "npt":
      ["501","502","508","509","510","521","504","505","507","520","512","511","513","522","524","525","515","516","514","523","526"],
    "sks":
      [{"id":101,"lv":"1"},{"id":100,"lv":"1"},{"id":102,"lv":"1"}],
   "wt":"3",
   "lu":{"fl":0},
   "re":{"c":-1},
   "ti":1263460473,
   "sn":"happyRanchZone",
   "cmd":1002
  }
 }
}

json2.txt文件内容为:

{
 "list":
 {
  "key":"fkey",
  "class":"compeditor",
  "caption":"公司信息",
  "showtype":1,
  "script":{
   "main":{"key":11100003,"step":1}
  }
 },
 "editer":
 {
  "caption":"公司信息"
 }

相关文章
|
Web App开发 JSON 数据库
使用delphi 开发多层应用(十七)使用RTC web 服务器返回JSON
RTC 作为delphi 的最专业的web 应用服务器,如果客户端要使用JSON 的话,那么使用RTC 应该也是一种 非常好的选择。下面我们做一个使用RTC web 服务器返回数据库JSON 的例子。
1902 0
|
SQL JSON 数据格式
使用delphi 开发多层应用(十二)使用kbmMW webserver 返回JSON
由于kbmMW 服务器与客户端交互是使用它的专用数据格式,因此与其它语言及平台相互访问的话就会有兼容问题, 为了最大化的利用kbmMW 服务器,从kbmMW 4.0 以后,其开始支持JSON 格式传递数据,其实就是可以通过 Webserver根据客户端的request 来返回对应的JSON串.
1034 0
|
前端开发 JavaScript 数据格式
Delphi调用JavaScript解析JSON
Delphi调用JavaScript解析Json 1.首先加入ComObj单元2.加入函数functin RunJs(const JsCode,JsVar:string):string;var script:OleVariant;begin try  Script :=CreateOleObject('ScriptControl');  Script.
1090 0
|
JSON 数据格式
SuperObject(Delphi最好的JSON简析类) 扩展功能----排序(1)
一直有个想法。SuperObject这么好用的Json简析类,序列化出来的Json排序是乱的。 有时候想用SuperObject做保存树数据。这样一来排序都是乱乱的也就放弃了,这几天想改造一下SuperObject类。
1232 0
|
JSON 数据格式
SuperObject(Delphi最好的JSON简析类) 扩展功能----排序(2)
在superObject中添加排序类型 type //add By Mofen  TSOSortMode = (sosmDefault {默认的方式}, sosmAdd {添加的顺序}, sosmASC {升序}, sosmDesc {降序});   添加全局设置方法 var  nowSor...
966 0
|
JSON 数据格式
SuperObject(Delphi最好的JSON简析类) 扩展功能----排序(3)
function TSuperAvlTree.Search(const k: SOString; st: TSuperAvlSearchTypes): TSuperAvlEntry;var  cmp, target_cmp: integer;  match_h, h: TSuperAvlEntry;  ha: Cardinal;begin  ha := TSuperAvlEntry.
1131 0
|
JSON 数据格式 存储
SuperObject(Delphi最好的JSON简析类) 扩展功能----排序(4)最终揭秘 解决sosmAdd模式不能查找的问题
经过对代码的更深入的跟踪理解,发现了superobject采用的是平衡二叉树的方式保存数据的。   首先看看保存数据的类 TSuperAvlEntry = class   private     FGt, FLt: TSuperAvlEntry; FGt和FLt分别是保存通过比较(比较hash或者比较key的asc)大的保存在Gt,小的保存在Lt,是一个二叉树链表。
1010 0
|
XML JSON API
Delphi XE程序设计系列 1-主从架构, 多层到JSON和REST
Delphi XE程序设计系列 1-主从架构, 多层到JSON和REST      从桌面开发,主从架构,一直到多层架构,虽然都是广泛被接受的观念和技术,但在信息技术的实作上却从不是开放, 相容的世界。
1544 0
|
JSON 数据格式
李维 《使用Delphi开发分散式JSON应用系统》
李维 《使用Delphi开发分散式JSON应用系统》 下载
1217 0
|
17天前
|
数据采集 JSON 数据处理
抓取和分析JSON数据:使用Python构建数据处理管道
在大数据时代,电商网站如亚马逊、京东等成为数据采集的重要来源。本文介绍如何使用Python结合代理IP、多线程等技术,高效、隐秘地抓取并处理电商网站的JSON数据。通过爬虫代理服务,模拟真实用户行为,提升抓取效率和稳定性。示例代码展示了如何抓取亚马逊商品信息并进行解析。
抓取和分析JSON数据:使用Python构建数据处理管道