为什么JSON反序列化在C#中失败?

为什么JSON反序列化在C#中失败?,json,serialization,json-deserialization,Json,Serialization,Json Deserialization,我正在尝试使用的Newtonsoft.Json dll反序列化字符串 运行时版本 v2.0.50727 版本 4.5.0.0 目标框架 .Net Framework 2.0 要设置字符串,请执行以下操作: jsonString ="{\"RelatedExceptions\":[{\"ClassName\":\"System.Web.Services.Protocols.SoapException\",\"Message\":\"Exception of type Microsoft.Share

我正在尝试使用的Newtonsoft.Json dll反序列化字符串 运行时版本 v2.0.50727

版本 4.5.0.0

目标框架 .Net Framework 2.0

要设置字符串,请执行以下操作:

jsonString ="{\"RelatedExceptions\":[{\"ClassName\":\"System.Web.Services.Protocols.SoapException\",\"Message\":\"Exception of type Microsoft.SharePoint.SoapServer.SoapServerException was thrown.\",\"Data\":{\"code\":{\"Namespace\":\"http://schemas.xmlsoap.org/soap/envelope/\",\"Name\":\"Server\",\"IsEmpty\":false},\"actor\":\"\",\"role\":\"\",\"subCode\":null,\"lang\":\"\"},\"InnerException\":null,\"HelpURL\":null,\"StackTraceString\":\"   at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)\\r\\n   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)\\r\\n   at abc.Common.SPWebServicesHelper.SPS2003ListsWs.Lists.GetListItems(String listName, String viewName, XmlNode query, XmlNode viewFields, String rowLimit, XmlNode queryOptions) in d:\\\\DeployerWorkingCopy\\\\TzDE2 .net2\\\\abc.Common.SPWebServicesHelper\\\\Web References\\\\SPS2003ListsWs\\\\Reference.cs:line 484\\r\\n   at abc.Common.SPHandlerBase.WSWrappers.SPS2003ListsWsWrapper.GetListItems(String serviceUrl, String listName, String viewName, XmlNode query, XmlNode viewFields, String rowLimit, XmlNode queryOptions) in d:\\\\DeployerWorkingCopy\\\\TzDE2 .net2\\\\abc.Common.SPHandlerBase\\\\WebServicesWrappers\\\\SPS2003ListsWsWrapper.cs:line 215\\r\\n   at abc.Common.SPS2003Handler.SPS2003ContentsManager.GetListItems(Site site, String listName, String strViewName, XmlNode ndQuery, XmlNode ndViewFields, String strRowLimit, XmlNode ndQueryOptions, String strWebId) in d:\\\\DeployerWorkingCopy\\\\TzDE2 .net2\\\\abc.Common.SPS2003Handler\\\\SPS2003ContentsManager.cs:line 5447\\r\\n   at abc.Common.SPS2003Handler.SPSContentsManager.GetListItemCollectionPositionNext(SPList list, Folder folder, Int32 itemCountStartPosition, String orderBy) in d:\\\\DeployerWorkingCopy\\\\TzDE2 .net2\\\\abc.Common.SPS2003Handler\\\\SPSContentsManager.cs:line 8460\\r\\n   at abc.Common.SPS2003Handler.SPSContentsManager.GetItemsAndProperties(SPList list, Folder folder, PropertyCollection props, Boolean readVersions, Boolean readOnlyFolders, Boolean readSecurity, Boolean readDiscussionItems) in d:\\\\DeployerWorkingCopy\\\\TzDE2 .net2\\\\abc.Common.SPS2003Handler\\\\SPSContentsManager.cs:line 8569\",\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":\"8\\nReadResponse\\nSystem.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\\nSystem.Web.Services.Protocols.SoapHttpClientProtocol\\nSystem.Object[] ReadResponse(System.Web.Services.Protocols.SoapClientMessage, System.Net.WebResponse, System.IO.Stream, Boolean)\",\"HResult\":-2146233087,\"Source\":\"System.Web.Services\",\"WatsonBuckets\":null}],\"Resolution\":\"\",\"EntryType\":1,\"Description\":\"Failed to read items of list: http://spportal:33333/_layouts/1033/lstman.aspx\\r\\n\\n        \\n\\tList does not exist\\n\\n\\tThe page you selected contains a list that does not exist.  It may have been deleted by another user.  Click \\\"Home\\\" at the top of the page to return to your Web site.\\n\\t\\n        0x82000006\\n      \",\"TimeStamp\":\"2013-05-08T11:45:54.299965+05:45\"}"
反序列化方法调用

JsonConvert.DeserializeObject<ExecutionReportEntry>(jsonString);
阶级

public enum ExecutionReportEntryType
{
致命的,
错误,
警告
信息,
成功
}
公共接口IEExecutionReportEntry
{
字符串说明{get;}
ExecutionReportEntryType EntryType{get;}
IList RelatedExceptions{get;}
字符串解析{get;set;}
日期时间时间戳{get;}
}
公共类ExecutionReportEntry:IEExecutionReportEntry
{
私有只读列表_relatedExceptions=新列表();
公共IList相关例外
{
得到
{
//删除只读模式会导致反序列化过程中出现问题(用于将数据存储到ESENT数据库中)
返回相关异常;
}
}
公共虚拟字符串解析
{
获取{return string.Empty;}
集合{}
}
public ExecutionReportEntryType EntryType{get;private set;}
公共字符串说明{get;private set;}
公共日期时间时间戳{get;private set;}
public ExecutionReportEntry(ExecutionReportEntryType entryType,字符串描述)
:this(DateTime.Now,entryType,description,null)
{ }
public ExecutionReportEntry(ExecutionReportEntryType entryType,字符串描述,异常示例)
:this(DateTime.Now,entryType,description,ex)
{ }
[JsonConstructor]
public ExecutionReportEntry(日期时间戳、ExecutionReportEntryType entryType、字符串描述、异常示例)
{
EntryType=EntryType;
描述=描述;
时间戳=时间戳;
如果(ex!=null)
_相关例外。添加(ex);
}
}

对于(反)序列化程序,RelatedExceptions属性为“只读”

也向属性添加一个集

其他二传手也是如此


它们需要是公共的,序列化程序才能设置它们。

您的JSON文本看起来像404错误。您确定请求的URL是正确的吗?能否向我们展示
ExecutionReportEntry
类?字符串似乎是有效的JSON,因此类和JSONI添加的JSON之间可能不匹配。以更可读的格式添加的JSON越多,请查看。。ExecutionReportEntry@Aphelion:主要问题是在JSON反序列化期间。我们正在尝试反序列化包含异常对象作为属性的对象。
StackTrace:
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 256
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract, JsonConverter converter) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 230
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, String id) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 728
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, String id) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 679
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 462
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 245
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract, JsonConverter converter) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 230
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 126
   at Newtonsoft.Json.Serialization.JsonSerializerProxy.DeserializeInternal(JsonReader reader, Type objectType) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerProxy.cs:line 174
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:line 461
   at Newtonsoft.Json.Serialization.JsonFormatterConverter.Convert(Object value, Type type) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonFormatterConverter.cs:line 62
   at System.Runtime.Serialization.SerializationInfo.GetValueNoThrow(String name, Type type)
   at System.Exception..ctor(SerializationInfo info, StreamingContext context)
   at Void .ctor(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)(Object , Object[] )
   at Newtonsoft.Json.Serialization.DefaultContractResolver.<>c__DisplayClass9.<CreateISerializableContract>b__8(Object[] args) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\DefaultContractResolver.cs:line 634
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateISerializable(JsonReader reader, JsonISerializableContract contract, String id) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 865
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 473
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 245
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract, JsonConverter converter) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 230
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IWrappedCollection wrappedList, JsonReader reader, String reference, JsonArrayContract contract) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 816
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.<>c__DisplayClass1.<CreateAndPopulateList>b__0(IList l, Boolean isTemporaryListReference) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 759
   at Newtonsoft.Json.Utilities.CollectionUtils.CreateAndPopulateList(Type listType, Action`2 populateList) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Utilities\CollectionUtils.cs:line 242
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateList(JsonReader reader, String reference, JsonArrayContract contract) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 747
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String reference) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 510
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 247
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueProperty(JsonReader reader, JsonProperty property, JsonConverter propertyConverter, Object target, Boolean gottenCurrentValue, Object currentValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 222
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonReader reader, Type objectType) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 1101
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, ConstructorInfo constructorInfo, String id) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 999
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateAndPopulateObject(JsonReader reader, JsonObjectContract contract, String id) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 968
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 444
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 245
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract, JsonConverter converter) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 230
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 126
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:line 483
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:line 461
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\JsonConvert.cs:line 925
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\JsonConvert.cs:line 885
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) in c:\Dev\Releases\Working\GitHub\Newtonsoft.Json\Src\Newtonsoft.Json\JsonConvert.cs:line 842
   at abc.Common.Collection.EsentErEntries`1.GetByKey(Double key) in d:\DeployerWorkingCopy\TzDE2 .net2\abc.Common\Collection\EsentDbWrapper.cs:line 748
{
    "RelatedExceptions": [
        {
            "ClassName": "System.Web.Services.Protocols.SoapException",
            "Message": "Exception of type Microsoft.SharePoint.SoapServer.SoapServerException was thrown.",
            "Data": {
                "code": {
                    "Namespace": "http://schemas.xmlsoap.org/soap/envelope/",
                    "Name": "Server",
                    "IsEmpty": false
                },
                "actor": "",
                "role": "",
                "subCode": null,
                "lang": ""
            },
            "InnerException": null,
            "HelpURL": null,
            "StackTraceString": "   at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)\r\n   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)\r\n   at abc.Common.SPWebServicesHelper.SPS2003ListsWs.Lists.GetListItems(String listName, String viewName, XmlNode query, XmlNode viewFields, String rowLimit, XmlNode queryOptions) in d:\\DeployerWorkingCopy\\TzDE2 .net2\\abc.Common.SPWebServicesHelper\\Web References\\SPS2003ListsWs\\Reference.cs:line 484\r\n   at abc.Common.SPHandlerBase.WSWrappers.SPS2003ListsWsWrapper.GetListItems(String serviceUrl, String listName, String viewName, XmlNode query, XmlNode viewFields, String rowLimit, XmlNode queryOptions) in d:\\DeployerWorkingCopy\\TzDE2 .net2\\abc.Common.SPHandlerBase\\WebServicesWrappers\\SPS2003ListsWsWrapper.cs:line 215\r\n   at abc.Common.SPS2003Handler.SPS2003ContentsManager.GetListItems(Site site, String listName, String strViewName, XmlNode ndQuery, XmlNode ndViewFields, String strRowLimit, XmlNode ndQueryOptions, String strWebId) in d:\\DeployerWorkingCopy\\TzDE2 .net2\\abc.Common.SPS2003Handler\\SPS2003ContentsManager.cs:line 5447\r\n   at abc.Common.SPS2003Handler.SPSContentsManager.GetListItemCollectionPositionNext(SPList list, Folder folder, Int32 itemCountStartPosition, String orderBy) in d:\\DeployerWorkingCopy\\TzDE2 .net2\\abc.Common.SPS2003Handler\\SPSContentsManager.cs:line 8460\r\n   at abc.Common.SPS2003Handler.SPSContentsManager.GetItemsAndProperties(SPList list, Folder folder, PropertyCollection props, Boolean readVersions, Boolean readOnlyFolders, Boolean readSecurity, Boolean readDiscussionItems) in d:\\DeployerWorkingCopy\\TzDE2 .net2\\abc.Common.SPS2003Handler\\SPSContentsManager.cs:line 8569",
            "RemoteStackTraceString": null,
            "RemoteStackIndex": 0,
            "ExceptionMethod": "8\nReadResponse\nSystem.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\nSystem.Web.Services.Protocols.SoapHttpClientProtocol\nSystem.Object[] ReadResponse(System.Web.Services.Protocols.SoapClientMessage, System.Net.WebResponse, System.IO.Stream, Boolean)",
            "HResult": -2146233087,
            "Source": "System.Web.Services",
            "WatsonBuckets": null
        }
    ],
    "Resolution": "",
    "EntryType": 1,
    "Description": "Failed to read items of list: http://spportal:33333/_layouts/1033/lstman.aspx\r\n\n        \n\tList does not exist\n\n\tThe page you selected contains a list that does not exist.  It may have been deleted by another user.  Click \"Home\" at the top of the page to return to your Web site.\n\t\n        0x82000006\n      ",
    "TimeStamp": "2013-05-08T11:45:54.299965+05:45"
}
public enum ExecutionReportEntryType
{
    Fatal,
    Error,
    Warning,
    Info,
    Success
}
public interface IExecutionReportEntry
{
    string Description { get; }
    ExecutionReportEntryType EntryType { get; }
    IList<Exception> RelatedExceptions { get; }
    string Resolution { get; set; }
    DateTime TimeStamp { get; }
}
public class ExecutionReportEntry : IExecutionReportEntry
{
    private readonly List<Exception> _relatedExceptions = new List<Exception>();

    public IList<Exception> RelatedExceptions
    {
        get
        {
            // Removing the readonly mode as this causes problem during Deserializing process(which is used to store the data into the ESENT database)
            return _relatedExceptions;
        }
    }

    public virtual string Resolution
    {
        get { return string.Empty; }
        set { }
    }

    public ExecutionReportEntryType EntryType { get; private set; }

    public string Description { get; private set; }

    public DateTime TimeStamp { get; private set; }

    public ExecutionReportEntry(ExecutionReportEntryType entryType, string description)
        : this(DateTime.Now, entryType, description, null)
    { }

    public ExecutionReportEntry(ExecutionReportEntryType entryType, string description, Exception ex)
        : this(DateTime.Now, entryType, description, ex)
    { }

    [JsonConstructor]
    public ExecutionReportEntry(DateTime timeStamp, ExecutionReportEntryType entryType, string description, Exception ex)
    {
        EntryType = entryType;
        Description = description;
        TimeStamp = timeStamp;

        if (ex != null)
            _relatedExceptions.Add(ex);
    }
}