procedure TForm1.btn1Click(Sender: TObject);
var
JSONStr: string;
JSONValue: TJSONValue;
Bridge: TJSONToDataSetBridge;
Adaptor: TJSONToDataSetBridge.IAdaptor;
begin
try
// 示例 JSON:数组 of 对象
JSONStr := '[' + '{"ID": 1, "Name": "Alice", "Age": 30, "Active": true},' +
'{"ID": 2, "Name": "Bob", "Age": 25, "Active": false}' + ']';
JSONValue := TJSONObject.ParseJSONValue(JSONStr);
try
// 创建适配器
// Adaptor := IAdaptor.Create;
Bridge := TJSONToDataSetBridge.Create(Adaptor);
try
// 绑定 FieldDefs 和 DataSet
Bridge.FieldDefs := fdmt1.FieldDefs;
Bridge.Dataset := fdmt1;
// 设置模式(推荐 Rich + ObjectView=False 简单场景)
Bridge.TypesMode := TJSONTypesMode.Rich;
Bridge.ObjectView := False; // 不展开嵌套(本例无嵌套)
// 第一步:定义字段结构
Bridge.Define(JSONValue);
// 应用字段定义
fdmt1.CreateDataSet;
// 第二步:导入数据
// Bridge.PKFields.
Bridge.Append(JSONValue);
fdmt1.Open;
finally
Bridge.Free;
end;
finally
JSONValue.Free;
end;
except
on E: Exception do
MessageDlg(E.ClassName+': '+E.Message, mtError, [mbOK],0);
end;
end;
