JSON反序列化到C# - 试图把JSON关联数组到字典<字符串,字符串> [英] Deserializing JSON to C# - Trying to turn JSON associative array into Dictionary<string, string>
问题描述
我有这样的JSON:
{
"AutoRefreshEnabled" : false,
"AutoRefreshInterval" : 1,
"AutoCycleEnabled" : false,
"AutoCycleInterval" : 1,
"Tabs" : {
"RadTab_Home",
"Dashboard"
},
"CommandName" : "Update Global Settings"
}
我试图将其存储在这个类,但我不知道如何处理嵌入标签的对象。有可能是分页大于0(因此1+,第一个的键总是被RadTab_Home)更大的任意数。标签不应该是的String []
。我想这是一个词典<字符串,字符串方式>
,但我不知道该如何来表达
I'm trying to store it in this class, but I am not sure how to handle the embedded Tabs object. There may be an arbitrary number of tabs greater than 0 (so 1+, the first one's key always being RadTab_Home). Tabs shouldn't be a string[]
. I want it to be a Dictionary<string, string>
, but I am unsure how to express that.
[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; }
}
编辑:标签现在不返回任何数据,但不会引发错误。
编辑:DataContractJsonSerializer不支持反序列化到一个字典。 JSON.net,但是,确实!
编辑:代码工作完全使用Newtonsoft的JSON解串器
Tabs does not return any data now, but no error is thrown. DataContractJsonSerializer does not support deserializing to a dictionary. JSON.net, however, does! Code worked perfectly using Newtonsoft's JSON deserializer.
推荐答案
如果你想在标签
属性是词典<字符串,字符串>
那么你在JSON表示不正确。目前,您有:
If you want the Tabs
property to be a Dictionary<string, string>
then your representation in JSON is incorrect. Currently, you have:
"Tabs" : [
"RadTab_Home",
"Dashboard"
],
和它应该是一个的String []
。如果你想要一个映射(即词典<字符串,字符串>
),那么你需要的键的与价值相关联,因此,在JSON不同的表示:
And it should be a string[]
. If you want a mapping (i.e. Dictionary<string, string>
), then you need a key to associate with the values, and therefore, a different representation in JSON:
"Tabs" : [
{ "key1" : "RadTab_Home" },
{ "key2" : "Dashboard" }
],
有了这个,你绝对可以创建一个词典<字符串,字符串>
,因为你将有一个关键的价值观联系起来。关键是创建一个类,像这样:
With this, you can definitely create a Dictionary<string, string>
, as you would have a key to associate with the values. The key would be to create a class, like so:
// 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; }
}
,然后定义你的标签
属性,如下:
[DataMember]
public Pair[] Tabs { get; set; }
您可以轻松地转换为词典<字符串,字符串>
使用LINQ:
// Deserialized instance.
MyClass instance = ...;
// Map
IDictionary<string, string> tabsMap = instance.Tabs.
ToDictionary(p => p.Key, p => p.Value);
您可以将其添加为你的类中的方法,但是这是为你做出设计决策(我,因为我认为这是一个数据传输对象)没有将它添加到类。
You could add it as a method on your class, but that's a design decision for you to make (I didn't add it to the class because I assume this is a data-transfer object).
这篇关于JSON反序列化到C# - 试图把JSON关联数组到字典<字符串,字符串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!