码迷,mamicode.com
首页 > Web开发 > 详细

通用对象转换Json格式

时间:2015-07-25 10:38:38      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

     public static string ObjectToJson<T>(IList<T> IL, params string[] args)
        {
            var Json = new StringBuilder();
            Json.Append("[");
            if (IL.Count > 0)
            {
                for (int i = 0; i < IL.Count; i++)
                {
                    Type type = IL[i].GetType();

                    Json.Append("{");
                    for (int x = 0; x < args.Length; x++)
                    {
                        var propertyInfo = type.GetProperty(args[x]);
                        Json.Append("\"" + propertyInfo.Name + "\":\"" + propertyInfo.GetValue(IL[i], null) + "\"");

                        if (x < args.Length - 1)
                        {
                            Json.Append(",");
                        }
                    }

                    Json.Append("}");
                    if (i < IL.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

 

通用对象转换Json格式

标签:

原文地址:http://www.cnblogs.com/domo/p/4675400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!