Json序列化成实体

  public ActionResult Index()
        {
            string strfile = "aaa.txt";
            string xpath = Server.MapPath("~/" + strfile);
            //FileStream myfs = new FileStream(xpath, FileMode.Open);
            //myfs.Close();
            StreamReader sr = new StreamReader(xpath, System.Text.Encoding.Default);
            String input = sr.ReadToEnd();
            sr.Close();
            //ViewBag.Texts = input;
            //JArray ja = (JArray)JsonConvert.DeserializeObject(input);
            //第一种方法
            //DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List));
            //MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
            //object ob = serializer.ReadObject(stream);
            //List list = (List)ob;
            //第二种方法
            List  modlist = JsonConvert.DeserializeObject>(input);

            return View();
        }

        public class modeldel
        {
            public int id { get; set; }

            public string text { get; set; }
            public string state { get; set; }

            public List children { get; set; }
        }



你可能感兴趣的:(.net)