zoukankan      html  css  js  c++  java
  • 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("
    ", string.Empty).Replace("
    ", string.Empty).Replace("	", string.Empty);
                }
                return content;
            }
    

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

            public FilePathResult getData3()
            {
                return new FilePathResult("~/Content/test.json", "application/json");
            }
  • 相关阅读:
    C++学习笔记1——const
    反转二叉树
    pywinauto 使用
    pywinauto 的使用
    爬虫基础知识
    mongdb安装配置
    pyinstaller
    Python3.6+pyinstaller+Django
    py2exe安装使用
    cx_freeze的安装使用
  • 原文地址:https://www.cnblogs.com/net-sky/p/11821605.html
Copyright © 2011-2022 走看看