2016-03-28 22 views
1

başarısız başarısız Bazı REST hizmetlerini çağıran ve C# nesneleri içine alıyorum sonuçları serileştirme yapıyorum. Her şey kitabına uygun hemen hemen arar ve çoğunlukla her şey gayet için, ancak deserializer kapalı atar ve çok yararlı hata mesajı verir benim JSON biri bölümü var:Newtonsoft.Json, geçersiz JSON

Beklenmeyen belirteç deserializing nesneyi : StartObject. Yol '1 channels.results', satır 1, pozisyon

636. yüzden gitti ve neyin yanlış olduğunu anlamaya olabilir görmek için benim kod ve JSON o kısmı izole ve şu anda bu var: https://dotnetfiddle.net/RWpwtn Bu neden olabilir ne

using System; 
using Newtonsoft.Json; 
using System.Collections.Generic; 

public class Program 
{ 
    public static void Main() 
    { 
     string JSON = "{\"id\" : 19,\"name\" : \"Vidensdeling\",\"sortOrder\" : 0,\"breakRightsInheritance\" : false,\"canContainItems\" : false,\"external\" : false,\"canOnlyEditItsOwn\" : null,\"parentChannelId\" : null,\"inheritedParentChannelId\" : null,\"itemCount\" : 0,\"permissionTarget\" : null,\"permissionTargetType\" : null,\"childChannelIds\" : [],\"contentTypeIds\" : [],\"mandatoryPrincipalIds\" : [],\"suggestedPrincipalIds\" : [],\"readRightsPrincipalIds\" : [],\"writeRightsPrincipalIds\" : [],\"visibleInCurrentView\" : null,\"mandatoryForCurrentUser\" : null,\"suggestedForCurrentUser\" : null,\"personalSelectedForCurrentUser\" : null,\"writeAllowedForCurrentUser\" : null,\"readAllowedForCurrentUser\" : null}"; 
     ChannelDtoV1 obj = JsonConvert.DeserializeObject<ChannelDtoV1>(JSON); 
     Console.WriteLine(obj.Id); 
    } 
} 

public class ChannelDtoV1 
{ 
    //Data properties 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int SortOrder { get; set; } 
    public bool BreakRightsInheritance { get; set; } 
    public bool CanContainItems { get; set; } 
    public bool External { get; set; } 
    public bool? CanOnlyEditItsOwn { get; set; } 
    public int? ParentChannelId { get; set; } 
    public int? InheritedParentChannelId { get; set; } 
    public int ItemCount { get; set; } 
    public string PermissionTarget { get; set; } 
    [JsonConverter(typeof(KVPEnumConverter<PermissionTargetType>))] 
    public PermissionTargetType? PermissionTargetType { get; set; } 

    //Collection properties 
    public ICollection<int> ChildChannelIds { get; set; } 
    public ICollection<int> ContentTypeIds { get; set; } 
    public ICollection<int> MandatoryPrincipalIds { get; set; } 
    public ICollection<int> SuggestedPrincipalIds { get; set; } 
    public ICollection<int> ReadRightsPrincipalIds { get; set; } 
    public ICollection<int> WriteRightsPrincipalIds { get; set; } 

    //Computed properties 
    public bool? VisibleInCurrentView { get; set; } 
    public bool? MandatoryForCurrentUser { get; set; } 
    public bool? SuggestedForCurrentUser { get; set; } 
    public bool? PersonalSelectedForCurrentUser { get; set; } 
    public bool? WriteAllowedForCurrentUser { get; set; } 
    public bool? ReadAllowedForCurrentUser { get; set; } 

    public ChannelDtoV1() 
    { 
     ChildChannelIds = new List<int>(); 
     MandatoryPrincipalIds = new List<int>(); 
     SuggestedPrincipalIds = new List<int>(); 
     ReadRightsPrincipalIds = new List<int>(); 
     WriteRightsPrincipalIds = new List<int>(); 
     ContentTypeIds = new List<int>(); 
    } 
} 

public enum PermissionTargetType 
{ 
    BussinessApp = 1 
} 

public class KVPEnumConverter<T> : JsonConverter 
{ 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     writer.WriteStartObject(); 
     writer.WritePropertyName("name"); 
     writer.WriteValue(value.ToString()); 
     writer.WritePropertyName("value"); 
     writer.WriteValue((int)value); 
     writer.WriteEndObject(); 
    } 

    public override bool CanConvert(Type objectType) 
    { 
     return objectType is T; 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     object rtnObj = null; 
     while (reader.Read() && reader.TokenType != JsonToken.EndObject) 
     { 
      if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "value") 
      { 
       reader.Read(); // proceed to value 
       rtnObj = Enum.Parse(typeof(T), reader.Value.ToString()); 
      } 
     } 
     return rtnObj ?? default(T); 
    } 
} 

kimse olduğu konusunda bir bilginiz var mı: (bir dinamik döküm en azından büyük bir kısmı, iş gibi görünüyor alırsa olsa) da başarısız olan?

+0

dotnetfiddles böylece linkrot olabilir siz de sorunuzun içinde tüm kodu yapıştırın (ama elbette keman bağlantısını tutmak) olmalıdır . – Default

+0

Bunu akılda tutmaya çalışacağım – bnm12

cevap

0

Hatayı buldum, sanırım bu yazı sadece ihtiyacım olan motivasyondu.

Hata, değer boşsa geri dönmek yerine KVPEnumConverter öğesinden çok ileriye doğru kaynaklanıyor. Yapmam gereken tek şey böyle bir boş çek tanıtmak oldu:

&& reader.TokenType != JsonToken.Null 
İlgili konular