DataTable转JSON
public static string ToJson(DataTable dt)
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
{
Dictionary<string, object> dictionary = new Dictionary<string, object>(); //实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
}
//JsonConvert 需要引用Newtonsoft.Json
public static string DataTableToJsonWithJsonNet(DataTable dt)
{
string JsonString = string.Empty;
JsonString = JsonConvert.SerializeObject(dt);
return JsonString;
}
public static string DataTableToJson(DataTable table)
{
var JsonString = new StringBuilder();
if (table.Rows.Count > 0)
{
JsonString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
JsonString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JsonString.Append("}");
}
else
{
JsonString.Append("},");
}
}
JsonString.Append("]");
}
return JsonString.ToString();
}
Json转DataTable
public static DataTable ToDataTable( string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > 0)
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == 0)
{
result = dataTable;
return result;
}
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch (Exception ex)
{
}
result = dataTable;
return result;
}
Json对象遍历 //需要引用 Newtonsoft.Json、Newtonsoft.Json.Linq
JObject jObject = JObject.Parse(conditionStr);
foreach (JProperty jProperty in jObject.Properties())
{
whereStr += " and " + jProperty.Name + " ='" + jProperty.Value + "' ";
Console.WriteLine("key:" + jProperty.Name + " value:" + jProperty.Value);
}
//xml转Json 需要引用Newtonsoft.Json
//Xml字符串转Json字符串
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
//json转xml
//json转xml
//添加引用System.Runtime.Serialization
string sJson="你的Json格式字符串";
XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(sJson), XmlDictionaryReaderQuotas.Max);
XmlDocument doc = new XmlDocument();
doc.Load(reader);