/// <summary>
/// 将JSON解析成DataSet只限标准的JSON数据
/// 例如:Json={t1:[{name:'数据name',type:'数据type'}]} 或 Json={t1:[{name:'数据name',type:'数据type'}],t2:[{id:'数据id',gx:'数据gx',val:'数据val'}]}
/// </summary>
/// <param name="Json">Json字符串</param>
/// <returns>DataSet</returns>
public static DataSet JsonToDataSet(string Json)
{
try
{
DataSet ds = new DataSet();
JavaScriptSerializer JSS = new JavaScriptSerializer();
object obj = JSS.DeserializeObject(Json);
Dictionary<string, object> datajson = (Dictionary<string, object>)obj;
foreach (var item in datajson)
{
DataTable dt = new DataTable(item.Key);
object[] rows = (object[])item.Value;
foreach (var row in rows)
{
Dictionary<string, object> val = (Dictionary<string, object>)row;
DataRow dr = dt.NewRow();
foreach (KeyValuePair<string, object> sss in val)
{
if (!dt.Columns.Contains(sss.Key))
{
dt.Columns.Add(sss.Key.ToString());
dr[sss.Key] = sss.Value;
}
else
dr[sss.Key] = sss.Value;
}
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
}
return ds;
}
catch
{
return null;
}
}
/// <summary>
/// 将DataSet转化成JSON数据
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static string DataSetToJson(DataSet ds)
{
string json = string.Empty;
try
{
if (ds.Tables.Count == 0)
throw new Exception("DataSet中Tables为0");
json = "{";
for (int i = 0; i < ds.Tables.Count; i++)
{
json += "T" + (i + 1) + ":[";
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{
json += "{";
for (int k = 0; k < ds.Tables[i].Columns.Count; k++)
{
json += ds.Tables[i].Columns[k].ColumnName + ":'" + ds.Tables[i].Rows[j][k].ToString() + "'";
if (k != ds.Tables[i].Columns.Count - 1)
json += ",";
}
json += "}";
if (j != ds.Tables[i].Rows.Count - 1)
json += ",";
}
json += "]";
if (i != ds.Tables.Count - 1)
json += ",";
}
json += "}";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return json;
}
/// 将JSON解析成DataSet只限标准的JSON数据
/// 例如:Json={t1:[{name:'数据name',type:'数据type'}]} 或 Json={t1:[{name:'数据name',type:'数据type'}],t2:[{id:'数据id',gx:'数据gx',val:'数据val'}]}
/// </summary>
/// <param name="Json">Json字符串</param>
/// <returns>DataSet</returns>
public static DataSet JsonToDataSet(string Json)
{
try
{
DataSet ds = new DataSet();
JavaScriptSerializer JSS = new JavaScriptSerializer();
object obj = JSS.DeserializeObject(Json);
Dictionary<string, object> datajson = (Dictionary<string, object>)obj;
foreach (var item in datajson)
{
DataTable dt = new DataTable(item.Key);
object[] rows = (object[])item.Value;
foreach (var row in rows)
{
Dictionary<string, object> val = (Dictionary<string, object>)row;
DataRow dr = dt.NewRow();
foreach (KeyValuePair<string, object> sss in val)
{
if (!dt.Columns.Contains(sss.Key))
{
dt.Columns.Add(sss.Key.ToString());
dr[sss.Key] = sss.Value;
}
else
dr[sss.Key] = sss.Value;
}
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
}
return ds;
}
catch
{
return null;
}
}
/// <summary>
/// 将DataSet转化成JSON数据
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static string DataSetToJson(DataSet ds)
{
string json = string.Empty;
try
{
if (ds.Tables.Count == 0)
throw new Exception("DataSet中Tables为0");
json = "{";
for (int i = 0; i < ds.Tables.Count; i++)
{
json += "T" + (i + 1) + ":[";
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{
json += "{";
for (int k = 0; k < ds.Tables[i].Columns.Count; k++)
{
json += ds.Tables[i].Columns[k].ColumnName + ":'" + ds.Tables[i].Rows[j][k].ToString() + "'";
if (k != ds.Tables[i].Columns.Count - 1)
json += ",";
}
json += "}";
if (j != ds.Tables[i].Rows.Count - 1)
json += ",";
}
json += "]";
if (i != ds.Tables.Count - 1)
json += ",";
}
json += "}";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return json;
}