zoukankan      html  css  js  c++  java
  • C#中List集合转换JSON

    #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

    结伴旅游网www.jieberu.com

    推推族www.tuituizu.com

    Tale、车车
  • 相关阅读:
    递推数列
    大数阶乘
    成绩排序
    DevC++ return 1 exit status
    POJ 1061 青蛙的约会
    ZOJ 2750 Idiomatic Phrases Game
    nyoj 545 Metric Matrice
    nyoj 308 Substring
    nyoj 515完全覆盖 II
    nyoj 1248 海岛争霸
  • 原文地址:https://www.cnblogs.com/taleche/p/4193646.html
Copyright © 2011-2022 走看看