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

第一种方法:

        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字符串

上一篇:JavaScript之事件委托(附原生js和jQuery代码)


下一篇:windows 使用 virtualbox,搭建 minikube 环境