#region Convert list<> to JSON
public string List2json (List<object> objlist, string classname)
{
String result = "{";
if (classname. Equals (String. Empty)//If there is no name for the given class, self-made smart
{
Object o = objlist[0];
classname = O.gettype (). ToString ();
}
result + = "\" "+ classname +" \ ": [";
BOOL Firstline = true;//The first line does not precede the "," number
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
Travel Network www.jieberu.com
Push-Push family www.tuituizu.com
The List collection transformation JSON in C #