夜微凉、的博客

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、首先确保程序集中添加了  System.Web.Extensions    DLL引用

2、代码中添加命名空间:using System.Web.Script.Serialization;

namespace JavescripSerializer
{
    class Program
    {
        static void Main(string[] args)
        {
            JavaScriptSerializer Script = new JavaScriptSerializer();
            User user = new User();
            user.id = 1;
            user.UserName = "王文建";
            user.Memo = "备注";
            user.Tel.Add(new TelPhone() { Numer = "18363963258" });
            user.Tel.Add(new TelPhone() { Numer = "18863936247" });
            string result = Script.Serialize(user);      //得到序列化json

            var User = Script.Deserialize<User>(result); //获得反序列化对象
        }
    }
    /// <summary>
    /// 用户类
    /// </summary>
    public class User
    {
        /// <summary>
        /// id
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 用户名称
        /// </summary>

        public string UserName { get; set; }
        /// <summary>
        /// 备注
        /// </summary>

        public string Memo { get; set; }
        /// <summary>
        /// 电话号码-一个用户可以多个号码
        /// </summary>

        public List<TelPhone> Tel { get; set; }

        public User()
        {
            Tel = new List<TelPhone>();
        }
    }
    /// <summary>
    /// 电话类
    /// </summary>
    public class TelPhone
    {
        /// <summary>
        /// 电话号码类
        /// </summary>
        public string Numer { get; set; }
    }
}

 

posted on 2016-09-18 16:26  夜、微凉  阅读(272)  评论(0编辑  收藏  举报