我正在尝试使用Newtonsoft.Json dll对字符串进行反序列化 运行时版本 V2.0.50727
版 4.5.0.0。
目标框架 .Net Framework 2.0
To String:
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);
结果: -
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
Json的格式更易读:
{
"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);
}
}
答案 0 :(得分:1)
对于(反)序列化程序,RelatedExceptions属性是'readonly'。
也为该属性添加一个集合。
其他私人制定者也是如此。
他们需要公开让序列化程序设置它们。