newtonsoft.json java,Newtonsoft.Json无法反序列化有效的JSON

我正在调用一些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);

}

}

有没有人知道可能导致这种情况的原因是什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值