用字典序列化/反序列化对象

时间:2019-11-22 13:56:56

标签: c# .net json wpf json.net

我对对象的序列化/反序列化有疑问。

我们有一个包含3个属性的类,其中一个是

    Dictionary <string, object1>. 

那个object1只有1个属性

    Dictionary<string, object2>

我们必须进行序列化/反序列化,因为我需要将其另存为字符串。

我正在尝试使用JsonConvert来完成

    string jsonObject = JsonConvert.SerializeObject(windowsProperties);

进行序列化和

    var rootObject = JsonConvert.DeserializeObject<WindowPropertiesM>(jsonObject);

反序列化对象。

反序列化时,字典保持为空。

有关如何操作的任何提示?尝试使用TypeNameHandling = TypeNameHandling。所有但没有帮助

编辑: 在SerializeObject之后,jsonObject看起来像这样

{
  "GridProperties": {
    "myGridControl": {
      "Properties": {
        "Number": {
          "SortIndex": -1,
          "SortOrder": 0,
          "Width": 66.0,
          "VisibleIndex": 11,
          "Visible": true
        },
        "Active": {
          "SortIndex": -1,
          "SortOrder": 0,
          "Width": 21.0,
          "VisibleIndex": 9,
          "Visible": true
        },
        "I": {
          "SortIndex": -1,
          "SortOrder": 0,
          "Width": 21.0,
          "VisibleIndex": 4,
          "Visible": true
        }
      }
    }
  },
  "MDISize": "1243,602",
  "MDILocation": "50,50"
}

编辑:

 public class WindowPropertiesM: BaseM
    {
        private Dictionary <string, Models.GridPropertiesM> _gridProperties;
        private System.Windows.Size _mdiSize;
        private System.Windows.Point _mdiLocation;

        public Dictionary <string, Models.GridPropertiesM> GridProperties
        {
            get { if (_gridProperties == null) _gridProperties = new Dictionary <string, Models.GridPropertiesM>(); return _gridProperties; }
            set { if (_gridProperties != value) { _gridProperties = value; } }
        }

        public System.Windows.Size MDISize
        {
            get { return _mdiSize; }
            set { if (_mdiSize != value) { _mdiSize = value; OnPropertyChanged(); } }
        }

        public System.Windows.Point MDILocation
        {
            get { return _mdiLocation; }
            set { if (_mdiLocation != value) { _mdiLocation = value; OnPropertyChanged(); } }
        }
    }



public class GridPropertiesM : BaseM
    {
        private Dictionary <string, Models.GridPropertyM> _Properties;

        public Dictionary <string, Models.GridPropertyM> Properties
        {
            get { if (_Properties == null) _Properties = new Dictionary <string, Models.GridPropertyM>(); return _Properties; }
            set { if (_Properties != value) { _Properties = value; } }
        }

    }
    public class GridPropertyM : BaseM
    {
        private int? _sortIndex;
        private DevExpress.Data.ColumnSortOrder _sortOrder;
        private double _width;
        private bool _visible;
        private int _visibleIndex;

        #region Properties
        public Int32? SortIndex
        {
            get { return _sortIndex; }
            set { if (_sortIndex != value) { _sortIndex = value; OnPropertyChanged(); } }
        }
        public DevExpress.Data.ColumnSortOrder SortOrder
        {
            get { return _sortOrder; }
            set { if (_sortOrder != value) { _sortOrder = value; OnPropertyChanged(); } }
        }
        public double Width
        {
            get { return _width; }
            set { if (_width != value) { _width = value; OnPropertyChanged(); } }
        }
        public Int32 VisibleIndex
        {
            get { return _visibleIndex; }
            set { if (_visibleIndex != value) { _visibleIndex = value; OnPropertyChanged(); } }
        }
        public bool Visible
        {
            get { return _visible; }
            set { if (_visible != value) { _visible = value; OnPropertyChanged(); } }
        }
        #endregion
    }

0 个答案:

没有答案