JSON反序列化对象无法按预期工作

时间:2017-10-02 18:23:25

标签: c# json wpf

一个类(稍后用作datacontext):

[Serializable]
public class CMiXData : INotifyPropertyChanged
{
    public CMiXData() { } 


    private ObservableCollection<string> _ChannelsSpriteCount = new ObservableCollection<string>(new[] { "1", "1", "1", "1", "1", "1" });
    public ObservableCollection<string> ChannelsSpriteCount
    {
        get { return _ChannelsSpriteCount; }
        set { _ChannelsSpriteCount = value; }
    }

某些usercontrol绑定到此类的属性ChannelsSpriteCount,并且序列化后保存的json可能如下所示:

{"ChannelsSpriteCount":["32","4","8","64","2","4"]}

当我以这种方式加载和反序列化json文件时:

    private void Load_Click(object sender, RoutedEventArgs e)
    {
        this.DataContext = JsonConvert.DeserializeObject<CMiXData>(File.ReadAllText(@"C:\Users\BabyClone\Documents\cmix.json"));
MessageBox.Show(cmixdata.ChannelsSpriteCount[0]);

无论我在保存的文件中看到什么值,加载的内容始终是原始类的默认值:在这种情况下"1" ...

我在这里缺少什么?

0 个答案:

没有答案