我正在调用一些REST服务并反序列化我进入C#对象的结果 . 一切看起来都很好,并且大部分内容都很好,但是我有一部分我的JSON会抛出反序列化器并提供不太有用的错误消息:
反序列化对象时出现意外的标记:StartObject . 路径'channels.results1',第1行,第636位 .
所以我去隔离了我的代码和JSON的一部分,看看我是否能弄清楚出了什么问题,而且目前我有这个:https://dotnetfiddle.net/RWpwtn也失败了(虽然如果它被投射到动态它似乎工作,至少对于大部分):
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(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))]
public PermissionTargetType? PermissionTargetType { get; set; }
//Collection properties
public ICollection ChildChannelIds { get; set; }
public ICollection ContentTypeIds { get; set; }
public ICollection MandatoryPrincipalIds { get; set; }
public ICollection SuggestedPrincipalIds { get; set; }
public ICollection ReadRightsPrincipalIds { get; set; }
public ICollection 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();
MandatoryPrincipalIds = new List();
SuggestedPrincipalIds = new List();
ReadRightsPrincipalIds = new List();
WriteRightsPrincipalIds = new List();
ContentTypeIds = new List();
}
}
public enum PermissionTargetType
{
BussinessApp = 1
}
public class KVPEnumConverter : 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);
}
}
有没有人知道可能导致这种情况的原因是什么?