首页 > Windows开发 > 详细

C# Newtonsoft.Json 读取文件,返回json字符串

时间:2019-09-16 21:55:45      阅读:130      评论:0      收藏:0      [点我收藏+]

第一种方法:

        public object getData2()
        {
            string content;
            using (StreamReader sr = new StreamReader(Server.MapPath("/Content/test.json")))
            {
                content = sr.ReadToEnd();
            }
            JsonSerializerSettings jSetting = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                DateFormatString = "yyyy-MM-dd HH:m:ss"
            };
            var jsonObject = JsonConvert.DeserializeObject<dynamic>(content, jSetting);
            Response.ContentType = "application/json";
            return jsonObject;
        }

第二种方法:

 
        public string getData()
        {
            string content;
            using (StreamReader sr = new StreamReader(Server.MapPath("/Content/test.json")))
            {
                content = sr.ReadToEnd().Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);
            }
            return content;
        }

第三种方法:直接返回json文件,设置返回类型ContentType为“application/json":

        public FilePathResult getData3()
        {
            return new FilePathResult("~/Content/test.json", "application/json");
        }

C# Newtonsoft.Json 读取文件,返回json字符串

原文:https://www.cnblogs.com/AlexanderZhao/p/11529950.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!