雪花

三、MVC_JsonResult类型

 

 

一、Ajax或者页面请求获取数据,不通过WebApi的时候,使用JsonResult作为返回Json数据格式的类型

二、代码呈现

    public class HomeController : Controller
    {
        /// <summary>
        /// 请求接口返回数据结构
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class ReqResult<T>
        {
            /// <summary>
            /// 提示消息
            /// </summary>
            public string message { get; set; }
            /// <summary>
            /// 状态码
            /// </summary>
            public string code { get; set; }
            /// <summary>
            /// 请求结果
            /// </summary>
            public T result { get; set; }

            /// <summary>
            /// 返回时间
            /// </summary>
            public string date { get; set; }
        }
        // GET: Home
        public JsonResult  Index()
        {
            var res = new ReqResult<object>();
            string date = "[{\"districtFullName\":\"黄浦区\",\"address\":\"打浦路38弄1-6号\",\"residentialareaName\":\"海华花园\",\"similarWord\":\"打浦路38弄1-6号\",\"communityId\":\"c2e08f29-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"闵行区\",\"address\":\"莘北路50弄\",\"residentialareaName\":\"森都公寓\",\"similarWord\":\"莘北路50弄\",\"communityId\":\"c2d7382d-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"浦东新区\",\"address\":\"东南路48弄1-10号\",\"residentialareaName\":\"东八公寓东区\",\"similarWord\":\"东南路74号\",\"communityId\":\"c2d73b7b-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"浦东新区\",\"address\":\"周东路368弄1-72号\",\"residentialareaName\":\"莲花二村\",\"similarWord\":\"周东路368弄51号\",\"communityId\":\"c2d742dd-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"浦东新区\",\"address\":\"周市路2045弄\",\"residentialareaName\":\"东南新村(周浦)\",\"similarWord\":\"东南新村5号\",\"communityId\":\"c2d74586-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"徐汇区\",\"address\":\"华泾路999弄1-373号\",\"residentialareaName\":\"上海漓江山水花园\",\"similarWord\":\"漓江花园一期\",\"communityId\":\"c2d74a64-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"浦东新区\",\"address\":\"东八灶115弄1-31号\",\"residentialareaName\":\"新育公寓\",\"similarWord\":\"新育公寓二期\",\"communityId\":\"c2d74c0e-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"闵行区\",\"address\":\"庙泾路52弄1号\",\"residentialareaName\":\"水清三村\",\"similarWord\":\"水清三村4号\",\"communityId\":\"c2d74cde-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"徐汇区\",\"address\":\"华济路2弄1-75号\",\"residentialareaName\":\"沙家浜小区\",\"similarWord\":\"华济路2弄37号\",\"communityId\":\"c2d74da8-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"},{\"districtFullName\":\"松江区\",\"address\":\"横港路49弄\",\"residentialareaName\":\"祥和公寓(松江)\",\"similarWord\":\"横港路49弄\",\"communityId\":\"c2d759c5-292c-11e5-ac2c-288023a0e898\",\"version\":\"2.0\"}]";
            res.code = "1";
            res.message = "请求成功";
            res.result = JsonConvert.DeserializeObject(date);
            return Json(res, JsonRequestBehavior.AllowGet);
        }
    }

 

三、解决方案

 

 

重点

List<Res> resList = JsonConvert.DeserializeObject<List<Res>>(result);

 

原由:你不能用了外部的ReqResult的model,里面的泛型T又用字符串,要么全是字符串,要么全是model。

 

posted @ 2019-05-14 11:42  十色  阅读(1747)  评论(0编辑  收藏  举报