2016-09-07 35 views
0

我有一個託管在Microsoft Azure雲中的應用程序。此應用程序是一項應用程序服務 我目前有一些JSON序列化的問題。收到的JSON格式不正確。 我的應用程序是一個ASP.NET MVC6。 基本上,我有一個前臺,他從控制器(服務器端)接收JSON值。JsonProperty似乎不適用於Windows Azure應用服務

C#服務器側方法:

[HttpGet] 
[Route("domain/search")] 
public IActionResult Search(string sn) 
{ 
    // DOING MY STUFF 
    ICollection<MyModel> myobject 
    return new ObjectResult(myobject); 
} 

模型類:

public class MyModel 
{ 
    [JsonProperty(PropertyName = "id", NullValueHandling = NullValueHandling.Ignore)] 
    public string Id { get; set; } 

    [JsonProperty(PropertyName = "sn", NullValueHandling = NullValueHandling.Ignore)] 
    public string SerialNumber { get; set; } 

    [JsonProperty(PropertyName = "state", NullValueHandling = NullValueHandling.Ignore)] 
    public AStateTypeModel? State { get; set; } 

    [JsonProperty(PropertyName = "description", NullValueHandling = NullValueHandling.Ignore)] 
    public string Description { get; set; } 

    . 
    . 
    ETC 
    . 
    . 
} 

正確JSON來接收:

[ 
    { 
     "id":"806c76b5-guid-guid-guid-guid", 
     "sn":"X00000000000", 
     "state":"AState", 
     . 
     . 
     ETC 
     . 
     . 
    } 
] 

當我以本地模式啓動應用程序時,一切都很好。我有正確的JSON值和屬性名稱。 但是在Azure上,前收到了不正確的JSON:

[ 
    { 
     "Id":"806c76b5-guid-guid-guid-guid", 
     "SerialNumber":"X00000000000", 
     "Description":null, 
     "State":1, 
     . 
     . 
     ETC 
     . 
     . 
    } 
] 

我認爲這是我的地方以及那些目前在Azure服務器之間的包版本的差異,但一切都看起來不錯。 JsonProperty屬性似乎被忽略。 我目前使用Windows Azure SDK 2.8,Newtonsoft 9.0.1。和ASPNET.MVC 6.0.0:

依賴關係:

"dependencies": { 
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", 
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*", 
    "Microsoft.AspNet.Mvc": "6.0.0-*", 
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-*", 
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-*", 
    "Microsoft.AspNet.StaticFiles": "1.0.0-*", 
    "Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*", 
    "Microsoft.AspNet.Diagnostics": "1.0.0-*", 
    "Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Owin": "1.0.0-rc1-final", 
    "Microsoft.AspNet.SignalR.Redis": "2.2.0", 
    "Microsoft.AspNet.WebSockets.Server": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final", 
    "Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-rc1-final", 
    "Newtonsoft.Json": "9.0.1" 
    } 

我不知道如果我有一些麻煩與JsonFormatter或類似的東西...... 怎麼了? 非常感謝您的幫助!

回答

0

我關閉後,因爲從昨天起,它的正常工作。 我什麼也沒做,除了玩包版: - Microsoft.AspNet.Mvc - Newtonsoft.Json 我想找到原因,但我缺乏線索。

0

在我看來,您的枚舉在本地模式下序列化爲string,並在部署到Azure時序列化爲int。您可以將[JsonConverter(typeof(StringEnumConverter))]屬性放在您的枚舉上,以確保它被序列化爲string

如果你想每默認string系列化,你可以設置Json.NET默認屬性,像這樣:

JsonConvert.DefaultSettings =() => new JsonSerializerSettings 
{ 
    Converters = 
    { 
     new StringEnumConverter 
     { 
      CamelCaseText = true 
     } 
    } 
}; 

爲了確保您的控制器並使用Json.NET,你可以重載標準Json()方法在控制器和使用Json(myobject),而不是ObjectResult(myobject)

protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) 
{ 
    return new JsonNetResult(base.Json(data, contentType, contentEncoding, behavior)); 
} 

private class JsonNetResult : JsonResult 
{ 
    public JsonNetResult() 
    { 
     this.ContentType = "application/json"; 
    } 

    public JsonNetResult(JsonResult existing) 
    { 
     this.ContentEncoding = existing.ContentEncoding; 
     this.ContentType = !string.IsNullOrWhiteSpace(existing.ContentType) ? existing.ContentType : "application/json"; 
     this.Data = existing.Data; 
     this.JsonRequestBehavior = existing.JsonRequestBehavior; 
    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
     { 
      throw new ArgumentNullException("context"); 
     } 
     if ((this.JsonRequestBehavior == JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) 
     { 
      base.ExecuteResult(context);       // Delegate back to allow the default exception to be thrown 
     } 

     HttpResponseBase response = context.HttpContext.Response; 
     response.ContentType = this.ContentType; 

     if (this.ContentEncoding != null) 
     { 
      response.ContentEncoding = this.ContentEncoding; 
     } 

     if (this.Data != null) 
     { 
      // Replace with your favourite serializer. 
      new Newtonsoft.Json.JsonSerializer().Serialize(response.Output, this.Data); 
     } 
    } 
} 
+0

這些問題不僅在枚舉上,而且在所有JSON屬性名稱上。 我想有 [ { 「ID」: 「806c76b5-GUID-GUID-GUID-GUID」, 「SN」: 「X00000000000」 } ] 代替 [ { 「Id」:「806c76b5-guid-guid-guid-guid」, 「SerialNumber」:「X00000000000」 } ] –

+0

現在我明白了。我更新了答案...也許天青並沒有使用Json.NET,所以你嘗試重載默認的Json響應方法, – Nico

+0

我試過了,但沒有奏效。我仍然有一個糟糕的JSON .... –

相關問題