unit FMX.DataSetToJSON;

interface
uses
FireDAC.Comp.Client,Data.DB; function DataSetToJSON(DataSet:TDataSet):String;
function JSONToDataSet(JSONTEXT:String):TFDMemTable; implementation
uses System.Rtti,System.JSON; function DataSetToJSON(DataSet:TDataSet):String;
var
I:integer;
JSONObject,FieldJSONObject:TJSONObject;
JSONArray:TJSONArray;
begin
Result:='';
JSONObject:=TJSONObject.ParseJSONValue('{}') as TJSONObject;
try
JSONObject.AddPair('Fields',TJSONArray.Create);
JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
for I := to DataSet.FieldDefs.Count - do
begin
FieldJSONObject:=TJSONObject.Create ;
FieldJSONObject.AddPair ('FieldName',DataSet.FieldDefs[i].Name );
FieldJSONObject.AddPair('DataType',TRttiEnumerationType.GetName<TFieldType>(DataSet.FieldDefs[i].DataType));
FieldJSONObject.AddPair('DataSize',TJSONNumber.Create(DataSet.FieldDefs[i].Size));
JSONArray.Add(FieldJSONObject) ;
end;
DataSet.First ;
JSONObject.AddPair('DATAS',TJSONArray.Create);
JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
while not DataSet.Eof do
begin
FieldJSONObject:=TJSONObject.Create ;
for I := to DataSet.FieldDefs.Count - do
FieldJSONObject.AddPair(DataSet.FieldDefs[i].Name,DataSet.Fields[i].AsString);
JSONArray.Add(FieldJSONObject) ;
DataSet.Next ;
end;
Result := JSONObject.ToJSON ;
finally
JSONObject.Free ;
end;
end; function JSONToDataSet(JSONTEXT:String):TFDMemTable;
var
I,R:integer;
JSONObject,FieldJSONObject:TJSONObject;
JSONArray:TJSONArray;
DataName,DataType:String;
FieldType:TFieldType;
FieldSize:integer;
begin
Result:=TFDMemTable.Create(nil);
if JSONTEXT='' then Exit;
JSONObject:=TJSONObject.ParseJSONValue(JSONTEXT) as TJSONObject;
try
JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
for I := to JSONArray.size - do
begin
Result.FieldDefs.Add(((JSONArray.Get(i) as TJSONObject).GetValue('FieldName') as TJSONString).Value,
TRttiEnumerationType.GetValue<TFieldType>(((JSONArray.Get(i) as TJSONObject).GetValue('DataType') as TJSONString).Value),
((JSONArray.Get(i) as TJSONObject).GetValue('DataSize') as TJSONNumber).AsInt64);
end;
Result.CreateDataSet ;
JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
for I := to JSONArray.size - do
begin
Result.Append ;
for R := to Result.FieldDefs.Count - do
begin
FieldJSONObject:=(JSONArray.Get(i) as TJSONObject);
if FieldJSONObject=nil then continue; try
Result.FieldByName(Result.FieldDefs[R].Name).Value :=
(FieldJSONObject.GetValue(Result.FieldDefs[R].Name) as TJSONString).Value ; Except
end;
end;
Result.Post ;
end;
finally
JSONObject.Free ;
end;
end;
end.

DataSetToJSON的更多相关文章

  1. DataSetToJson 扩展方法

    001 using System; 002 using System.Collections.Generic; 003 using System.Linq; 004 using System.Text ...

  2. JSON和数据集互相转换单元

    如题......只是一个单元, 为了测试JSON单元性能的... 具体测试结果参考: http://www.cnblogs.com/hs-kill/p/3668052.html 代码中用到的Seven ...

  3. EasyUi – 6.easyui常见问题

    1.进度条 2.JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法 3. 三张表的连接查询现在到datagrid里 4.日期组合框DateBox设置readonly ...

  4. EasyUi – 4.datagrid

    测试的时候用Json来测试就好啦. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> ...

  5. mormot 数据集转换为JSON字串

    mormot 数据集转换为JSON字串 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graph ...

  6. C#中Json和List/DataSet相互转换

    #region List<T> 转 Json        /// <summary>        /// List<T> 转 Json        /// & ...

  7. ASP.NET 之 常用类、方法的超级总结,并包含动态的EXCEL导入导出功能,奉上类库源码

    实用类:UtilityClass 包含如下方法 判断对象是否为空或NULL,如果是空或NULL返回true,否则返回false 验证手机号是否正确 13,15,18 验证邮箱 验证网址 MD5加密,返 ...

  8. .NET DataTable转化为json格式

    标准的json用“分隔,不用' public static string DataSetToJson(DataTable dt) {        string json = string.Empty ...

  9. Ajax+存储过程真分页实例解析(10W数据毫秒级+项目解析)

    周末闲来无事,突然想写个分页的东西玩玩,说走就走 在文章最后我会把整个项目+数据库附上,下载下来直接运行就可以看效果了.整个项目采用的是简单三层模式,开发平开是VS2010+SQL2012 一.我要做 ...

随机推荐

  1. Javascript库的产生和解读

    javascript库的产生,增强了浏览器或javascript语言的某些机制的功能, 让业务开发人员,更专注业务逻辑,而不是机制逻辑.   比如, 0.兼容性 同样的功能函数,不同的浏览器所暴露的a ...

  2. 【Django】【六】接口自动化测试框架

    我的源码地址:https://github.com/woshixiaoyu202017/djangoTest 详细构建步骤如下 1. 生成新的测试数据库的数据库表结构guest_test 2. 数据库 ...

  3. hdu 1573 X问题 两两可能不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...

  4. python flask 接口

    例子1 from flask import Flask, jsonify app = Flask(__name__) tasks = [ { , 'title': u'Buy groceries', ...

  5. c++ 查找数组或者容器元素是否存在(find)

    #include <iostream> // cout #include <algorithm> // find #include <vector> // vect ...

  6. ubuntu 16.04 u盘挂载以及卸载

    1.列出所有磁盘 sudo fdisk -l 2.最后一段信息显示的为u盘 Device Boot Start End Sectors Size Id Type /dev/sdb4 * 256 786 ...

  7. 【转】xml节点解析成字符串的方法

    网址:http://blog.csdn.net/shanzhizi/article/details/8817532 ZC: 这是 libxml2的 之前汇总了一篇关于xml文档与字符串转换的文章,文章 ...

  8. 《A_Pancers》第一次作业:团队亮相

    一.团队及团队成员介绍 1> 队名:A_Pancers 2> 团队成员组成: 201571030310/龙正圆(小组长) 201571030329/杨环宇      20157103030 ...

  9. PHP自带调试函数

    1.var_dump:打印变量的相关信息 $a = array(1, 2, array("a", "b", "c")); var_dump( ...

  10. MyBatis中的@Mapper注解 @Mappe与@MapperScan关系

    从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件 现在项目中的配置 public interface DemoMapper{ int deleteByPr ...