C# 使用JsonConvert C反序列化包含字典对象的对象#
我正在尝试序列化下面定义的对象。使用Newtonsoft JsonConvert可以正确地实现这一点。这将返回一个字符串。 当我尝试将字符串反序列化回已定义的对象时,它不起作用并引发异常C# 使用JsonConvert C反序列化包含字典对象的对象#,c#,deserialization,C#,Deserialization,我正在尝试序列化下面定义的对象。使用Newtonsoft JsonConvert可以正确地实现这一点。这将返回一个字符串。 当我尝试将字符串反序列化回已定义的对象时,它不起作用并引发异常 private class PlotSetFeatureStateInfo { public IDictionary<int,IDictionary<AxisTypeAndUnitInfo,ManualScaleInfo>> Persist
private class PlotSetFeatureStateInfo
{
public IDictionary<int,IDictionary<AxisTypeAndUnitInfo,ManualScaleInfo>> PersistentScaleInfo
{
get;
set;
}
public IDictionary<Guid,ScaleType> PlotIdVsLocalScaleType { get; set; }
public IDictionary<Guid,IDictionary<AxisTypeAndUnitInfo,
PlotScales.PersistentScaleData>> PlotIdVsPersistentScaleData { get; set;}
}
var foo = new FeatureStateInfo
{
//Fill values
};
var res = JsonConvert.SerializeObject(foo);
var deserializedProperty = JsonConvert.DeserializeObject<FeatureStateInfo>(res);//Throws error
解决方案:
我根据大家的建议修改了课程,如下所示。
这对我来说很好
public class PlotSetFeatureStateInfo
{
public List<KeyValuePair<int, List<KeyValuePair<AxisTypeAndUnitInfo, ManualScaleInfo>>>> PersistentScaleInfo
{
get;
set;
}
public List<KeyValuePair<Guid, ScaleType>>PlotIdVsLocalScaleType { get; set; }
public List<KeyValuePair<Guid,List<KeyValuePair<AxisTypeAndUnitInfo,
PlotScales.PersistentScaleData>>>> PlotIdVsPersistentScaleData { get; set; }
}
公共类PlotSetFeatureStateInfo
{
公共列表PersistentScaleInfo
{
得到;
设置
}
公共ListPlotIdVsLocalScaleType{get;set;}
公共列表PlotIdVsPersistentScaleData{get;set;}
}
谢谢:)您可以发布您正在尝试反序列化的示例
JSON
吗?显示JSON字符串以及您尝试反序列化的方式。请看这里@Sagar Deshpande您是否检查了JSON。这似乎是无效的。@LeonidMalyshev,这篇文章谈到了XmlSerializer。我正在为包装在object中的字典寻找Json反序列化。您是否可以发布您尝试反序列化的示例Json
?显示Json字符串以及您尝试反序列化的方式。看这里@Sagar Deshpande您是否检查了Json。这似乎是无效的。@LeonidMalyshev,这篇文章谈到了XmlSerializer。我正在寻找包装在对象中的字典的Json反序列化
public class PlotSetFeatureStateInfo
{
public List<KeyValuePair<int, List<KeyValuePair<AxisTypeAndUnitInfo, ManualScaleInfo>>>> PersistentScaleInfo
{
get;
set;
}
public List<KeyValuePair<Guid, ScaleType>>PlotIdVsLocalScaleType { get; set; }
public List<KeyValuePair<Guid,List<KeyValuePair<AxisTypeAndUnitInfo,
PlotScales.PersistentScaleData>>>> PlotIdVsPersistentScaleData { get; set; }
}