我有一個託管在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
或類似的東西...... 怎麼了? 非常感謝您的幫助!
這些問題不僅在枚舉上,而且在所有JSON屬性名稱上。 我想有 [ { 「ID」: 「806c76b5-GUID-GUID-GUID-GUID」, 「SN」: 「X00000000000」 } ] 代替 [ { 「Id」:「806c76b5-guid-guid-guid-guid」, 「SerialNumber」:「X00000000000」 } ] –
現在我明白了。我更新了答案...也許天青並沒有使用Json.NET,所以你嘗試重載默認的Json響應方法, – Nico
我試過了,但沒有奏效。我仍然有一個糟糕的JSON .... –