一、JSON简介
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。
1、JSON 语法规则
- 数据在名称/值对中
- 数据由逗号分隔
- 大括号 { } 保存对象
- 中括号 [ ] 保存数组,数组可以包含多个对象
JSON 数据的书写格式是:key : value - 名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:“name” : “名称”
2、JSON值类型
- 数字(整数或浮点数):
{ "age":30 } - 字符串(在双引号中):
{ "name":"名称" } - 逻辑值(true 或 false):
{ "flag":true } - 数组(在中括号中):下面例子中,对象 sites 是包含三个对象的数组。每个对象代表一条关于某个网站(name、url)的记录。
{
"sites": [
{ "name":"百度" , "url":"www.baidu.com" },
{ "name":"google" , "url":"www.google.com" },
{ "name":"微博" , "url":"www.weibo.com" }
]
}
- 对象(在大括号中):上面例子都是对象
- null:JSON 可以设置 null 值
{ "value":null }
二、XSuperObject使用教程
在Delphi中,可以使用XSuperObject单元来处理JSON。 开源地址:https://github.com/spinettaro/delphi-event-bus 挑几个常见用法介绍下:
1、基础用法???
{
"name": "Onur YILDIZ",
"vip": true,
"telephones": ["000000000", "111111111111"],
"age": 24,
"size": 1.72,
"adresses": [
{
"adress": "blabla",
"city": "Antalya",
"pc": 7160
},
{
"adress": "blabla",
"city": "Adana",
"pc": 1170
}
]
}
在Delphi中实现上方JSON对象
var
X: ISuperObject;
begin
X := SO;
X.S['name'] := 'Onur YILDIZ';
X.B['vip'] := true;
with X.A['telephones'] do
begin
Add('000000000');
Add('111111111111');
end;
X.I['age'] := 24;
X.F['size'] := 1.72;
with X.A['adresses'].O[0] {Auto Create} do
begin
S['adress'] := 'blabla';
S['city'] := 'Antalya';
I['pc'] := 7160;
end;
X.A['adresses'].O[1].S['adress'] := 'blabla';
X.A['adresses'].O[1].S['city'] := 'Adana';
X.A['adresses'].O[1].I['pc'] := 1170;
2、Super Expressions 表达式
const
JSON = '{ "o": { '+
' "1234567890": {'+
' "last use date": "2010-10-17T01:23:20",'+
' "create date": "2010-10-17T01:23:20",'+
' "name": "iPhone 8s"'+
' }'+
' },'+
' "Index": 0, '+
' "Data": {"Index2": 1}, '+
' "a": [{'+
' "last use date": "2010-10-17T01:23:20",'+
' "create date": "2010-11-17T01:23:20",'+
' "name": "iPhone 8s",'+
' "arr": [1,2,3] '+
' }, '+
' {'+
' message: "hello"'+
' }]'+
'}';
var
X: ISuperObject;
NewJSon: ISuperObject;
NewArray: ISuperArray;
begin
X := SO(JSON);
ShowMessage( X['o."1234567890"."last use date"'].AsString );
ShowMessage( X['a[Index]."create date"'].AsString );
ShowMessage( X['a[Data.Index2].message'].AsString );
X['a[0].arr'].AsArray.Add('test1');
NewJSON := X['{a: a[Index], b: a[Data.Index2].message, c: o."1234567890".name, d: 4, e: a[0].arr[2], f: " :) "}'].AsObject;
NewArray := X['[a[Index], a[Data.Index2].message, Data.Index2, Index, 1, "1", "test"]'].AsArray;
end;
3、Where 过滤
var
FilterJSON: ISuperObject;
begin
FilterJSON := SO('{ Table: [ '+
' { '+
' Name: "Sakar SHAKIR", ' +
' Sex: "M", ' +
' Size: 1.75 '+
' }, '+
' { '+
' Name: "Bulent ERSOY", ' +
' Sex: "F", ' +
' Size: 1.60 '+
' }, '+
' { '+
' Name: "Cicek ABBAS", ' +
' Sex: "M", ' +
' Size: 1.65 '+
' } '+
' ] '+
'}');
Memo1.Lines.Add(
FilterJSON.A['Table'].Where(function(Arg: IMember): Boolean
begin
with Arg.AsObject do
Result := (S['Sex'] = 'M') and (F['Size'] > 1.60)
end).AsJSON
);
end;
4、Delete 删除
var
FilterJSON: ISuperObject;
begin
FilterJSON := SO('{ Table: [ '+
' { '+
' Name: "Sakar SHAKIR", ' +
' Sex: "M", ' +
' Size: 1.75 '+
' }, '+
' { '+
' Name: "Bulent ERSOY", ' +
' Sex: "F", ' +
' Size: 1.60 '+
' }, '+
' { '+
' Name: "Cicek ABBAS", ' +
' Sex: "M", ' +
' Size: 1.65 '+
' } '+
' ] '+
'}');
Memo1.Lines.Add(
FilterJSON.A['Table'].Delete(function(Arg: IMember): Boolean
begin
with Arg.AsObject do
Result := (S['Sex'] = 'M') and (F['Size'] > 1.60)
end).AsJSON
);
end;
5、Sorting 排序
var
X: ISuperObject;
A: ISuperArray;
begin
X := SO('{b:1, a:2, d:4, c:2}');
ShowMessage(X.AsJSON);
X.Sort(function(Left, Right: IMember): Integer begin
Result := CompareText(Left.Name, Right.Name);
end);
ShowMessage(X.AsJSON);
A := SA('[{index:3}, {index:4}, {index:2}, {index:1}]');
ShowMessage(A.AsJSON);
A.Sort(function(Left, Right: IMember): Integer begin
Result := CompareValue(Left.AsObject.I['index'], Right.AsObject.I['index']);
end);
ShowMessage(A.AsJSON);
end;
6、Variant 可变类型
var
X: ISuperObject;
begin
X := TSuperObject.Create;
X.V['A'] := 1;
X.V['B'] := '2';
X.V['C'] := 1.3;
X.V['D'] := False;
X.V['E'] := Null;
X.V['F'] := Now;
Memo1.Lines.Add(X.AsJSON);
end;
7、Loops 循环??
const
JSN = '{ '+
' "adresses": [ '+
' { '+
' "adress": "blabla", '+
' "city": "Antalya", '+
' "pc": 7160 '+
' },'+
' { '+
' "adress": "blabla", '+
' "city": "Adana", '+
' "pc": 1170 '+
' } '+
' ] '+
'}';
var
X, Obj: ISuperObject;
J: Integer;
begin
X := TSuperObject.Create(JSN);
with X.A['adresses'] do
for J := 0 to Lenght -1 do
begin
Obj := O[J];
Obj.First;
while not Obj.EoF do
begin
Memo1.Lines.Add( Obj.CurrentKey + ' = ' + VarToStr(Obj.CurrentValue.AsVariant));
Obj.Next;
end;
Memo1.Lines.Add('------');
end;
end;
|