C#中List集合转换JSON
2016-10-28 09:18
465 查看
#region 将List<>转换为Json
public string List2JSON(List<object> objlist, string classname)
{
string result = "{";
if (classname.Equals(string.Empty))//如果没有给定类的名称,那么自做聪明地安一个
{
object o = objlist[0];
classname = o.GetType().ToString();
}
result += "\"" + classname + "\":[";
bool firstline = true;//处理第一行前面不加","号
foreach (object oo in objlist)
{
if (!firstline)
{
result = result + "," + OneObjectToJSON(oo);
}
else
{
result = result + OneObjectToJSON(oo) + "";
firstline = false;
}
}
return result + "]}";
}
private string OneObjectToJSON(object o)
{
string result = "{";
List<string> ls_propertys = new List<string>();
ls_propertys = GetObjectProperty(o);
foreach (string str_property in ls_propertys)
{
if (result.Equals("{"))
{
result = result + str_property;
}
else
{
result = result + "," + str_property + "";
}
}
return result + "}";
}
private List<string> GetObjectProperty(object o)
{
List<string> propertyslist = new List<string>();
PropertyInfo[] propertys = o.GetType().GetProperties();
foreach (PropertyInfo p in propertys)
{
propertyslist.Add("\"" + p.Name.ToString() + "\":\"" + p.GetValue(o, null) + "\"");
}
return propertyslist;
}
#endregion
public string List2JSON(List<object> objlist, string classname)
{
string result = "{";
if (classname.Equals(string.Empty))//如果没有给定类的名称,那么自做聪明地安一个
{
object o = objlist[0];
classname = o.GetType().ToString();
}
result += "\"" + classname + "\":[";
bool firstline = true;//处理第一行前面不加","号
foreach (object oo in objlist)
{
if (!firstline)
{
result = result + "," + OneObjectToJSON(oo);
}
else
{
result = result + OneObjectToJSON(oo) + "";
firstline = false;
}
}
return result + "]}";
}
private string OneObjectToJSON(object o)
{
string result = "{";
List<string> ls_propertys = new List<string>();
ls_propertys = GetObjectProperty(o);
foreach (string str_property in ls_propertys)
{
if (result.Equals("{"))
{
result = result + str_property;
}
else
{
result = result + "," + str_property + "";
}
}
return result + "}";
}
private List<string> GetObjectProperty(object o)
{
List<string> propertyslist = new List<string>();
PropertyInfo[] propertys = o.GetType().GetProperties();
foreach (PropertyInfo p in propertys)
{
propertyslist.Add("\"" + p.Name.ToString() + "\":\"" + p.GetValue(o, null) + "\"");
}
return propertyslist;
}
#endregion
相关文章推荐
- C# Json格式转换成List集合
- c# list集合转换成json数据
- C#、JSON、JS List转换为JSON在前台获取使用
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- C#中Json和List/DataSet相互转换
- [Json] C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json (转载)
- 实体转json串。能满足普通list的转换。。及一些些简单的对象.对象.属性的转换《还未完善--对象.集合》
- c# Json List DataTable 数据相互转换
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
- c# Json List<T> DataTable 数据相互转换
- 使用xStream对(Java对象、List集合、Map )到JSON转换、从JSON转换java对象
- C# json字符串 转换成数组、集合、相应对象
- C#将Json字符串反序列化成List对象类集合
- c# Json List<T> DataTable 数据相互转换
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
- c# 任意类型转换成json (datable,list,array 等)
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json
- c# Json List<T> DataTable 数据相互转换
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- C# List集合转换成DataTable