0

我正在使用ASP.NET MVC 5 Web Api。Web API:未能序列化內容類型的響應正文

我現在有很多API的應用程序。 最近我已經實現了自定義的JsonConverter,它將按照時區轉換Date。 「發生了一個錯誤。」, 「ExceptionMessage」:

public class CustomInfoConverter : JsonConverter 
{ 

    public override bool CanConvert(Type objectType) 
    { 
     return objectType == typeof(CustomType); 
    } 
    public override bool CanRead 
    { 
     get 
     { 
      return false; 
     } 
    } 
    public override bool CanWrite 
    { 
     get 
     { 
      return true; 
     } 
    } 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     var customType = (CustomType)value; 
     if (customType == null || null== customType.TimeZone) return; 
     //DateTime currentDateTime = customType.Date??DateTime.Now; 
     DateTime currentDateTime = DateTime.SpecifyKind(customType.Date ?? DateTime.Now, DateTimeKind.Unspecified); 

     DateTime userDateTime = TimeZoneInfo.ConvertTimeFromUtc(currentDateTime, customType.TimeZone); 
     customType.Date = userDateTime; 
     JsonSerializer innerSerializer = new JsonSerializer(); 
     foreach (var converter in serializer.Converters.Where(c => !(c is CustomInfoConverter))) 
     { 
      innerSerializer.Converters.Add(converter); 
     } 
     innerSerializer.Serialize(writer, customType); 


    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 


} 

實現這一習俗JsonConverter所有的API,不同的是一個API,它是拋出異常下面

{ 「消息」 工作之後的「 'ObjectContent`1'類型未能序列化響應正文 內容類型'application/json; charset = utf-8'。「,」ExceptionType「:」System.InvalidOperationException「,」StackTrace「:null,」InnerException 「:{」Message「:」發生了 錯誤。「,」ExceptionMessage「:」狀態爲的令牌屬性名稱屬性會導致無效的JSON對象。路徑 'Data.Forms [0]' 「」 ExceptionType。 「:」 Newtonsoft.Json.JsonWriterException 「 」堆棧跟蹤「:」 在Newtonsoft.Json.JsonWriter.AutoComplete(JsonToken tokenBeingWritten個)\ r \ n在 Newtonsoft.Json.JsonWriter.InternalWritePropertyName(字符串名稱)\ r \ n 在Newtonsoft.Json.JsonTextWriter.WritePropertyName(字符串名稱, 布爾逃逸)\ r \ n在 Newtonsoft.Json.Serialization.JsonProperty.WritePropertyName(JsonWriter 作者)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty個)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 作家,對象的值,JsonContract valueContract,JsonProperty構件, JsonContainerContract containerContract,JsonProperty containerProperty個)\ r \ n在 Newtonsoft.Json。 Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 作家,IEnumerable的值,JsonArrayContract合同,JsonProperty 構件,JsonContainerContract collectionContract,JsonProperty containerProperty個)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 作家,對象的值, JsonContract valueContract,JsonProperty成員, JsonContainerContract containerContract,JsonProperty containerProperty個)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter 作家,對象的值,JsonObjectContract合同,JsonProperty 構件,JsonContainerContract collectionContract,JsonProperty containerProperty個)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 作家,對象的值,JsonContract valueContract,JsonProperty構件, JsonContainerContract containerContract,JsonProperty containerProperty個)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter。SerializeValue(JsonWriter 作家,對象的值,JsonContract valueContract,JsonProperty構件, JsonContainerContract containerContract,JsonProperty containerProperty個)\ r \ n在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,對象的值,類型的objectType)\ r \ñ在 Newtonsoft.Json.JsonSerializer.SerializeInternal(jsonWriter jsonWriter,對象的值,類型的objectType個)\ r \ n在 Newtonsoft.Json.JsonSerializer.Serialize(jsonWriter jsonWriter,對象 值)\ r \ n在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(類型 類型,對象值,流writeStream,編碼 effectiveEncoding個)\ r \ n在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(類型 類型,對象的值,流writeStream,編碼 effectiveEncoding個)\ r \ n在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter .WriteToStream(類型 類型,對象的值,流writeStream,HttpContent內容個)\ r \ n在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(類型 類型,對象的值,流writeStream,HttpContent內容, TransportContext transportContext ,CancellationToken cancellationToken)\ r \ n ---以前位置的堆棧跟蹤結束 拋出異常--- \ r \ n在 System.Runtime.CompilerServices .TaskAwaiter.ThrowForNonSuccess(任務 任務個)\ r \ n在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務 任務個)\ r \ n在 System.Runtime.CompilerServices.TaskAwaiter.GetResult(個)\ r \ n在 System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()「}}

您可以參考這個link瞭解更多詳情。

回答

0

的問題看起來是,在某些情況下,你是從WriteJson()返回不寫任何東西,特別是當customType.TimeZone == null

var customType = (CustomType)value; 
    if (customType == null || null== customType.TimeZone) return; 

這樣做會導致一個無效的JSON對象,因爲物業將已經寫入調用者,從而導致:

{ 
    "customType": 
} 

試圖做到這一點會導致您看到的例外情況。

相反,您需要防止屬性本身被序列化。這在轉換器中是不可能的,但它需要在包含類型中完成。

爲了避免序列化的屬性與空值,你應該設置NullValueHandling = NullValueHandling.Ignoreserializer settings,或對物業本身:

public class ContainerClass 
{ 
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] 
    public CustomType CustomType { get; set; } 
} 

爲了防止序列化的屬性時,其TimeZone屬性爲null,你應該使用conditional property serialization通過在包含類型中添加ShouldSerializeXXX()方法,其中XXX與您的房產名稱完全匹配:

public class ContainerClass 
{ 
    public CustomType CustomType { get; set; } 

    public bool ShouldSerializeCustomType() 
    { 
     return CustomType != null && CustomType.TimeZone != null; 
    } 
} 
相關問題