tnblog
首页
视频
资源
登录

对象转JSON

926人阅读 2018/11/29 15:39 总访问:926 评论:0 收藏:0 手机
分类: JSON

        /// <summary>

        /// 对象转json

        /// </summary>

        public static string ToJson(object jsonObject)

        {

            StringBuilder sb = new StringBuilder();

            sb.Append("{"


);



            PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();

            for (int i = 0; i < propertyInfo.Length; i++)

            {

                object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);

                Type type = propertyInfo[i].PropertyType;

                string strValue = objectValue.ToString();

                strValue = StringFormat(strValue, type);

                sb.Append("\"" + propertyInfo[i].Name + "\":");

                sb.Append(strValue + ",");

            }

            sb.Remove(sb.Length - 1, 1);

            sb.Append("}");

            return sb.ToString();

        }

    


评价
没有个性,不需要签名
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术