WebApi返回Json格式字符串


WebApi返回json格式字符串, 在網上能找到好幾種方法, 其中有三種普遍的方法, 但是感覺都不怎么好. 

先貼一下, 網上給的常用方法吧.

方法一:(改配置法) 

找到Global.asax文件,在Application_Start()方法中添加一句:

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    WebApiConfig.Register(GlobalConfiguration.Configuration); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    // 使api返回為json 
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); 
} 

這樣返回的結果就都是json類型了,但有個不好的地方,如果返回的結果是String類型,如123,返回的json就會變成"123";


解決的方法是自定義返回類型(返回類型為HttpResponseMessage)

public HttpResponseMessage PostUserName(User user) 
{ 
    String userName = user.userName; 
    var result = new HttpResponseMessage{ Content = new StringContent(userName,Encoding.GetEncoding("UTF-8"), "application/json")}; 
    return result; 
} 

 

方法二:(萬金油法) 

方法一中又要改配置,又要處理返回值為String類型的json,甚是麻煩,不如就不用webapi中的的自動序列化對象,自己序列化后再返回

public HttpResponseMessage PostUser(User user) 
{ 
    JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    string str = serializer.Serialize(user); 
    HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; 
    return result; 
} 

為了不在每個接口中都反復寫那幾句代碼,所以就封裝為一個方法這樣使用就方便多了。

public static HttpResponseMessage toJson(Object obj) 
{ 
    String str; 
    if (obj is String ||obj is Char) 
    { 
        str = obj.ToString(); 
    } 
    else 
    { 
        JavaScriptSerializer serializer = new JavaScriptSerializer(); 
        str = serializer.Serialize(obj); 
    } 
    HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; 
    return result; 
}   

 

方法三:(最麻煩的方法) 

方法一最簡單,但殺傷力太大,所有的返回的xml格式都會被斃掉,那么方法三就可以只讓api接口中斃掉xml,返回json 

先寫一個處理返回的類:

public class JsonContentNegotiator : IContentNegotiator 
{ 
    private readonly JsonMediaTypeFormatter _jsonFormatter; 

    public JsonContentNegotiator(JsonMediaTypeFormatter formatter) 
    { 
        _jsonFormatter = formatter; 
    } 

    public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters) 
    { 
        var result = new ContentNegotiationResult(_jsonFormatter, new  MediaTypeHeaderValue("application/json")); 
        return result; 
    } 
}    

找到App_Start中的WebApiConfig.cs文件,打開找到Register(HttpConfiguration config)方法 

添加以下代碼:

var jsonFormatter = new JsonMediaTypeFormatter(); 
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); 

添加后代碼如下:

public static void Register(HttpConfiguration config) 
{ 
    config.Routes.MapHttpRoute( 
        name: "DefaultApi", 
        routeTemplate: "api/{controller}/{action}/{id}", 
        defaults: new { id = RouteParameter.Optional } 
    ); 
    var jsonFormatter = new JsonMediaTypeFormatter(); 
    config.Services.Replace(typeof(IContentNegotiator), new  JsonContentNegotiator(jsonFormatter)); 
} 

方法三如果返回的結果是String類型,如123,返回的json就會變成"123",解決方法同方法一。 

其實WebApi會自動把返回的對象轉為xml和json兩種格式並存的形式,方法一與方法三是斃掉了xml的返回,而方法二是自定義返回。

以上三種方法, 就是我找到的比較普遍的方法了. 但是總覺得並不是那么好. 都要改這改那的. 

還有一種方式能返回json格式字符串. 先看一下效果吧.

public class HomeController : ApiController
{
    [HttpGet]
    public JsonData Know(string msg)
    {
        msg = "WebApi 已接收到信息" ;
        return new JsonData() { Content = new List<string>() { "a", "b", "c" }, IsSuccess = true, Message = msg };
    }

    public List<string> Get()
    {
        return new List<string>() { "a", "b", "c"};
    }
}

看的出來, Know方法返回的是 json 格式的字符串, Get方法, 返回的是xml格式的.

從上面來看, 主要是返回值不一樣. 那么JsonData里面有什么秘密呢?

public class JsonData : ISerializable
{
    #region 屬性

    /// <summary>
    /// 表示業務是否正常
    /// </summary>
  public bool IsSuccess { get; set; } /// <summary> /// 返回消息,成功的消息和錯誤消息都在這里 /// </summary>
  public string Message { get; set; } /// <summary> /// 用於返回復雜結果 /// </summary>
  public object Content { get; set; } #endregion #region 方法 /// <summary> /// 自定義序列化方法 /// </summary> /// <param name="info"></param> /// <param name="context"></param> public void GetObjectData(SerializationInfo info, StreamingContext context) { // 運用info對象來添加你所需要序列化的項 info.AddValue("IsSuccess", IsSuccess); info.AddValue("Message", Message); if (Content != null) { info.AddValue("Content", Convert.ChangeType(Content, Content.GetType())); } else { info.AddValue("Content", null); } } public JsonData() { } #endregion }

這里主要是要實現 ISerializable 接口 .

 可能有人注意到, 我訪問api的時候, 路由模式和訪問mvc是一樣的, 其實這里很簡單, 只需要在webapi路由注冊哪里, 加入一個路由就可以了.

config.Routes.MapHttpRoute(
    name: "DefaultApi1",
    routeTemplate: "api/{controller}/{action}",
    defaults: new { id = RouteParameter.Optional }
);

這樣, 就加入了一個路由匹配規則進去. 只不過, 需要在Know方法上面, 加上一些訪問限制條件. 如httpget, 否則, 如果直接去訪問, 是不可以的.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM