gpt4 book ai didi

c# - 将 JSON 反序列化为类

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:32 27 4
gpt4 key购买 nike

我正在尝试反序列化这个字符串:

storage = "{\"1\":{\"1\":\"aaa\"},\"2\":{\"1\":\"bbb\"}}";

有效:

var LocalStorageOBJ1 = JsonConvert.DeserializeObject<Dictionary<DataModels.StoragePrimaryKeys,
Dictionary<DataModels.StorageSecondaryKeys, string>>>(storage);

但是我想用一个类来做,比如:

var LocalStorageOBJ = JsonConvert.DeserializeObject<MyClass>(storage);

MyClass 是:

public class DataModels
{
public enum StoragePrimaryKeys
{
Login = 1,
Account = 2
};

public enum StorageSecondaryKeys
{
JobTitle = 1,
JobId = 2,
JobLocation = 3,
RenewDate = 4,
ExpirationDate = 5
};
}


public class MyClass
{
public Dictionary<DataModels.StoragePrimaryKeys, ForiegnData> PrimaryDictionary { get; set; }
}
public class ForiegnData
{
public Dictionary<DataModels.StorageSecondaryKeys, string> ForeignDictionary { get; set; }
}

不幸的是,我在反序列化时得到了 null。

感谢任何帮助!

最佳答案

MyClass 不是

Dictionary<DataModels.StoragePrimaryKeys, Dictionary<DataModels.StorageSecondaryKeys, string>>

MyClass 有

Dictionary<DataModels.StoragePrimaryKeys, Dictionary<DataModels.StorageSecondaryKeys, string>>

所以你应该正确封装JSON字符串:

{\"PrimaryDictionary\" : {\"1\":{\"1\":\"aaa\"},\"2\":{\"1\":\"bbb\"}}} 

关于c# - 将 JSON 反序列化为类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com