json转dataset的另外一种解析方式自动生成guid强关联
Posted hn_lijia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json转dataset的另外一种解析方式自动生成guid强关联相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 将json字符串自动转成dataset,并且自动补全主子关联关系,
/// Guid,FKGuid
/// Author:lijia
/// date:2019-03-28
/// </summary>
/// <param name="schemaName"></param>
/// <param name="tbName">datatable名称</param>
/// <param name="jsonDS">dataset对象</param>
/// <param name="dsJs">json字符串</param>
/// <param name="ListToStringColumn">需要做特殊处理的字段,如字段a:["1","2"]不希望生成单独表</param>
public static void GetDsFromJson(string schemaName, string tbName, DataSet jsonDS, JObject dsJs, List<string> ListToStringColumn)
{
CreateTbByJson(tbName, jsonDS, dsJs, ListToStringColumn);
CreateTbDbByJson(tbName, "rtnTb", "", jsonDS, dsJs, ListToStringColumn);
if (!string.IsNullOrEmpty(schemaName))
{
foreach (DataTable item in jsonDS.Tables)
{
item.TableName = schemaName + "_" + item.TableName;
}
}
}
/// <summary>
/// 根据json组装dataset
/// </summary>
/// <param name="tbName">datatable名称</param>
/// <param name="jsonDS">dataset对象</param>
/// <param name="tbJs">json字符串</param>
/// <param name="ListToStringColumn">需要做特殊处理的字段,如字段a:["1","2"]不希望生成单独表</param>
private static void CreateTbByJson(string tbName, DataSet jsonDS, JObject tbJs, List<string> ListToStringColumn)
{
if (tbName == "") tbName = "rtnTb";
DataTable tb = new DataTable();
tb.TableName = tbName;
if (ListToStringColumn == null)
{
ListToStringColumn = new List<string>();
}
//循环json字符串组装表结构
foreach (var item in tbJs)
{
string fdname = item.Key;
if (ListToStringColumn.Contains(fdname))
{
if (!tb.Columns.Contains(fdname))
{
DataColumn dc = new DataColumn(fdname, typeof(string));
tb.Columns.Add(dc);
}
}
else
{
if (item.Value.Type != JTokenType.Array && item.Value.Type != JTokenType.Object)
{
if (!tb.Columns.Contains(fdname))
{
DataColumn dc = new DataColumn(fdname, typeof(string));
tb.Columns.Add(dc);
}
}
else
{
if (item.Value.Type == JTokenType.Array)
{
JArray rowsJA = item.Value as JArray;
//判断,如果rowsJA第一个元素不是OBJECT,则表示此数组为枚举值数组,则增加表,字段为F1
if (rowsJA.Count > 0 && rowsJA[0].Type != JTokenType.Object && rowsJA[0].Type != JTokenType.Array)
{
if (ListToStringColumn.Count <= 0 || (ListToStringColumn.Count > 0 && !ListToStringColumn.Contains(item.Key)))
{
JArray tmpRowsJA = new JArray();
for (int i = 0; i < rowsJA.Count; i++)
{
JObject tmpRowJO = new JObject();
tmpRowJO.Add("F1", rowsJA[i]);
tmpRowsJA.Add(tmpRowJO);
}
JObject jo = new JObject();
jo.Add(item.Key, tmpRowsJA);
CreateTbByJson(item.Key, jsonDS, jo, ListToStringColumn);
}
}
else
{
foreach (var ArrayItem in item.Value)
{
CreateTbByJson(item.Key, jsonDS, (JObject)ArrayItem, ListToStringColumn);
}
}
}
else
{
CreateTbByJson(item.Key, jsonDS, (JObject)item.Value, ListToStringColumn);
}
}
}
}
if (!tb.Columns.Contains("Guid"))
{
DataColumn dc = new DataColumn("Guid", typeof(string));
tb.Columns.Add(dc);
}
if (!tb.Columns.Contains("Fk_Guid"))
{
DataColumn dc = new DataColumn("Fk_Guid", typeof(string));
tb.Columns.Add(dc);
}
if (!string.IsNullOrEmpty(tbName) && !jsonDS.Tables.Contains(tbName)) //准备表结构
{
jsonDS.Tables.Add(tb);
}
}
/// <summary>
/// 把json数据转换成dataset
/// </summary>
/// <param name="tbName">datatable名称</param>
/// <param name="mtbName">主表名称</param>
/// <param name="strFGuid">Guid唯一键</param>
/// <param name="jsonDS">已经通过json组装好的dataset</param>
/// <param name="tbJs">json数据</param>
/// <param name="ListToStringColumn">需要做特殊处理的字段,如字段a:["1","2"]不希望生成单独表</param>
public static void CreateTbDbByJson(string tbName, string mtbName, string strFGuid, DataSet jsonDS, JObject tbJs, List<string> ListToStringColumn)
{
if (tbName == "") tbName = "rtnTb";
if (jsonDS.Tables.Contains(tbName))
{
DataRow dr = jsonDS.Tables[tbName].NewRow();
string strGuid = System.Guid.NewGuid().ToString();
//strFGuid = strFGuid + "+" + mtbName;
dr["Guid"] = strGuid;
dr["Fk_Guid"] = strFGuid;
foreach (var item in tbJs)
{
string fdname = item.Key;
if (ListToStringColumn.Contains(fdname))
{
dr[fdname] = item.Value.ToString().Replace("[", "").Replace("]", "").Replace(" ", "").Replace(""", "");
}
else
{
if (item.Value.Type != JTokenType.Array && item.Value.Type != JTokenType.Object)
{
if (!jsonDS.Tables[tbName].Columns.Contains(fdname))
{
DataColumn dc = new DataColumn(fdname, typeof(string));
jsonDS.Tables[tbName].Columns.Add(dc);
}
dr[fdname] = item.Value;
}
else
{
if (item.Value.Type == JTokenType.Array)
{
JArray rowsJA = item.Value as JArray;
//判断,如果rowsJA第一个元素不是OBJECT,则表示此数组为枚举值数组,则增加表,字段为F1
if (rowsJA.Count > 0 && rowsJA[0].Type != JTokenType.Object && rowsJA[0].Type != JTokenType.Array)
{
if (ListToStringColumn.Count <= 0 || (ListToStringColumn.Count > 0 && !ListToStringColumn.Contains(item.Key)))
{
JArray tmpRowsJA = new JArray();
for (int i = 0; i < rowsJA.Count; i++)
{
JObject tmpRowJO = new JObject();
tmpRowJO.Add("F1", rowsJA[i]);
tmpRowsJA.Add(tmpRowJO);
}
rowsJA = tmpRowsJA;
}
}
foreach (var ArrayItem in rowsJA)
{
CreateTbDbByJson(item.Key, tbName, strGuid, jsonDS, (JObject)ArrayItem, ListToStringColumn);
}
}
else
{
CreateTbDbByJson(item.Key, tbName, strGuid, jsonDS, (JObject)item.Value, ListToStringColumn);
}
}
}
}
if (null != dr)
{
jsonDS.Tables[tbName].Rows.Add(dr);
}
}
}
以上是关于json转dataset的另外一种解析方式自动生成guid强关联的主要内容,如果未能解决你的问题,请参考以下文章