将JSON反序列化为C#-尝试将JSON关联数组转换为Dictionary<;字符串,字符串>;

本文关键字:字符串 JSON lt gt Dictionary 数组 C#- 反序列化 关联 转换 | 更新日期: 2023-09-27 17:58:27

我有这个JSON:

{
    "AutoRefreshEnabled" : false,
    "AutoRefreshInterval" : 1,
    "AutoCycleEnabled" : false,
    "AutoCycleInterval" : 1,
    "Tabs" : {
        "RadTab_Home",
        "Dashboard"
    },
    "CommandName" : "Update Global Settings"
}

我试图将它存储在这个类中,但我不确定如何处理嵌入的Tabs对象。可能有任意数量的选项卡大于0(因此为1+,第一个选项卡的键总是RadTab_Home)。选项卡不应该是string[]。我希望它是Dictionary<string, string>,但我不确定如何表达。

[DataContract]
public class GlobalSettingsJSON
{
    private static readonly ILog Logger = 
        LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    public GlobalSettingsJSON() { }
    public GlobalSettingsJSON(string commandName, string autoRefreshEnabled, 
        string autoRefreshInterval, string autoCycleEnabled, 
        string autoCycleInterval, Dictionary<string, string> tabs)
    {
        Logger.InfoFormat("Command Name: {0}, DockID: {1}, " +
            "AutoRefreshEnabled: {2}, AutoRefreshInterval: {3}, " +
            "AutoCycleEnabled: {4}, AutoCycleInterval: {5}",
            commandName, autoRefreshEnabled, autoRefreshInterval, 
            autoCycleEnabled, autoCycleInterval);
        CommandName = commandName;
        AutoRefreshEnabled = autoRefreshEnabled;
        AutoRefreshInterval = autoRefreshInterval;
        AutoCycleEnabled = autoCycleEnabled;
        AutoCycleInterval = autoCycleInterval;
        Tabs = tabs;
    }
    [DataMember(Name = "CommandName")]
    public string CommandName { get; set; }
    [DataMember(Name = "AutoRefreshEnabled")]
    public string AutoRefreshEnabled { get; set; }
    [DataMember(Name = "AutoRefreshInterval")]
    public string AutoRefreshInterval { get; set; }
    [DataMember(Name = "AutoCycleEnabled")]
    public string AutoCycleEnabled { get; set; }
    [DataMember(Name = "AutoCycleInterval")]
    public string AutoCycleInterval { get; set; }
    [DataMember(Name = "Tabs")]
    public Dictionary<string, string> Tabs { get; set; }
}

EDIT:选项卡现在不返回任何数据,但不会引发任何错误。EDIT:DataContractJsonSerializer不支持反序列化为字典。然而,JSON.net确实如此!EDIT:代码使用Newtonsoft的JSON反序列化程序运行得很好。

将JSON反序列化为C#-尝试将JSON关联数组转换为Dictionary<;字符串,字符串>;

如果您希望Tabs属性是Dictionary<string, string>,那么您在JSON中的表示是不正确的。目前,您有:

"Tabs" : [
    "RadTab_Home",
    "Dashboard"
],

它应该是string[]。如果你想要一个映射(即Dictionary<string, string>),那么你需要一个来与值相关联,因此,在JSON中需要一个不同的表示:

"Tabs" : [
    { "key1" : "RadTab_Home" },
    { "key2" : "Dashboard" }
],

有了这个,您肯定可以创建一个Dictionary<string, string>,因为您将有一个与值关联的键。关键是创建一个类,如下所示:

// NOTE: You can use POCO DataContract serialization for this type.
[DataContract]
public class Pair
{
    [DataMember]
    public string Key { get; set; }
    [DataMember]
    public string Value { get; set; }
}

然后定义您的Tabs属性,如下所示:

[DataMember]
public Pair[] Tabs { get; set; }

您可以使用LINQ:轻松地将其转换为Dictionary<string, string>

// Deserialized instance.
MyClass instance = ...;
// Map
IDictionary<string, string> tabsMap = instance.Tabs.
    ToDictionary(p => p.Key, p => p.Value);

你可以把它作为一个方法添加到你的类中,但这是你的设计决定(我没有把它添加到类中,因为我认为这是一个数据传输对象)。