///<summary>/// Converts datatable to JSON string.///</summary>///<param name="dt"></param>///<returns></returns>privatestringDataTableToJson(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字符串
}
2.调用方法,传入一个DataTable就可以。哪个DataTable都可以的。
string json = DataTableToJson(dt);
4.如何序列化?
1.创建一个方法
///<summary>/// ConvertS an object to a JSON string When the string length is long///</summary>///<param name="o"></param>///<returns></returns>privatestringToJson2(Object o)
{
JavaScriptSerializer j = new JavaScriptSerializer();
j.MaxJsonLength = Int32.MaxValue;
return j.Serialize(o);
}
2.就这样传入一个对象就行了。
比如:Person p = new Person();string json = ToJson2(p);
上面在DataTable中也用到过序列化的Demo,可以参考用到大量数据时进行转JSON
List<Person> people = jss.Deserialize<List<Person>>(json);
4.通过映射,自动获取到JSON对应类(Class)的属性名,实现自动化并通过遍历输出
PropertyInfo[] piArr = typeof(Person).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (people.Count == 0)
{
DeserializeToObject(json);
return;
}
//Get the name and value of the specified class Person automatically.
foreach (var person in people)
{
sb.Clear();
foreach (PropertyInfo pi in piArr)
{
sb.Append(pi.Name + "=" + pi.GetValue(person));
sb.Append("\t ");
}
listAll.Items.Add(sb.ToString());
//listAll.Items.Add("name=" + person.Name + "\tid=" + person.Id + "\tphone=" + person.Phone);
}
下面是用到的方法
///<summary>/// Reading the specified file that contains a json string///</summary>///<param name="sender"></param>///<param name="e"></param>privatevoidbtnAnalyzeJson_Click(object sender, EventArgs e)
{
//Judge whether the specified file exists.if (File.Exists(filename))
{
//Getting the string that is a specified fileusing (StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8))
{
string json = sr.ReadToEnd();
//analyze the json string.
txtOrgJson.Text = json;
//The first method.(For one object)//Person p = jss.Deserialize(json,typeof(Person)) as Person;//txtAnalysedJson.Text = "name="+p.Name+"\r\nid="+p.Id+"\r\nphone="+p.Phone;//The second method.(For a lots objects)
List<Person> people = jss.Deserialize<List<Person>>(json);
StringBuilder sb = new StringBuilder();
PropertyInfo[] piArr = typeof(Person).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (people.Count == 0)
{
DeserializeToObject(json);
return;
}
//Get the name and value of the specified class Person automatically.foreach (var person in people)
{
sb.Clear();
foreach (PropertyInfo pi in piArr)
{
sb.Append(pi.Name + "=" + pi.GetValue(person));
sb.Append("\t ");
}
listAll.Items.Add(sb.ToString());
//listAll.Items.Add("name=" + person.Name + "\tid=" + person.Id + "\tphone=" + person.Phone);
}
}
}
else
{
MessageBox.Show("Cannot find the specified file.Please click the up button of this.");
}
}