The List collection transformation JSON in C #

Source: Internet
Author: User

#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 #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.