gpt4 book ai didi

c# - 将 JSON 反序列化为 C# - 尝试将 JSON 关联数组转换为 Dictionary

转载 作者:行者123 更新时间:2023-11-30 15:44:10 24 4
gpt4 key购买 nike

我有这个 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; }
}

编辑:Tabs 现在不返回任何数据,但不会抛出任何错误。编辑:DataContractJsonSerializer 不支持反序列化到字典。然而,JSON.net 可以!编辑:代码使用 Newtonsoft 的 JSON 反序列化器完美运行。

最佳答案

如果你想要 Tabs属性为 Dictionary<string, string>那么您在 JSON 中的表示不正确。目前,您有:

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

它应该是一个 string[] .如果你想要一个映射(即 Dictionary<string, string> ),那么你需要一个 key 来与值相关联,因此需要一个不同的 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; }

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

// Deserialized instance.
MyClass instance = ...;

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

您可以将它作为方法添加到您的类中,但这是您做出的设计决定(我没有将它添加到类中,因为我假设这是一个 data-transfer object )。

关于c# - 将 JSON 反序列化为 C# - 尝试将 JSON 关联数组转换为 Dictionary<string, string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6217916/

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