.ToDictionary KeyValuePair将键/值重命名为名称/值 json.net 输出



嘿,我有以下代码,我正在尝试将"KEY"和">VALUE"的默认值重命名为">名称"和"值">

public class jsonOutputStyle
{
public string Name { get; set; }
public string Value { get; set; }
}
[Obsolete]
public string POB_CODE()
{
Dictionary<string, string> _dicts = null;
try
{
using (OracleConnection Oconn = new OracleConnection(connectionORI))
{
_dicts = Oconn.Query<KeyValuePair<string, string>>(
"SELECT " +
"POB_CODE AS Key," +
"POB_DESC AS Value " +
"FROM " +
"POB_CODE " +
"WHERE " +
"DISPLAY_SORT_ORDER >=1 " +
"AND " +
"DISPLAY_SORT_ORDER <=60",
null
)
.ToDictionary(pair => new jsonOutputStyle() { 
Name = pair.Key, 
Value = pair.Value
});
}
}
catch (SqlException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return JsonConvert.SerializeObject(_dicts, Formatting.None);
}

这会产生以下错误:

错误 CS0029 无法将类型'System.Collections.Generic.Dictionary<WCF.Service.NNicsAPI.jsonOutputStyle, System.Collections.Generic.KeyValuePair<string, string>>'隐式转换为'System.Collections.Generic.Dictionary<string, string>'

因此,我不确定我需要做什么才能纠正问题,以便json输出如下所示:

[{"Name":"","Value":""},{"Name":"Female","Value":"F"},{"Name":"Male","Value":"M"}];

而不是这样的:

[{"key":"","value":""},{"key":"Female","value":"F"},{"key":"Male","value":"M"}];

试试这个:

_dicts = Oconn.Query<KeyValuePair<string, string>>(
"SELECT " +
"POB_CODE AS Key," +
"POB_DESC AS Value " +
"FROM " +
"POB_CODE " +
"WHERE " +
"DISPLAY_SORT_ORDER >=1 " +
"AND " +
"DISPLAY_SORT_ORDER <=60",
null
)
.ToDictionary(pair => pair.Key, pair => pair.Value);

我不明白你为什么要把"钥匙"命名为"名字"。当字典转换为 JSON 时,它将看起来像这样 {"actual_value_of_key" : "value"}。您不会看到写入的变量 Name。

编辑:如果你想要像[{"Name":"","Value":""},{"Name":"Female","Value":"F"},{"Name":"Male","Value":"M"}]这样的JSON输出,那么不要使用字典。使用您定义的类。

_dicts = Oconn.Query<jsonOutputStyle>(
"SELECT " +
"POB_CODE AS Name," +
"POB_DESC AS Value " +
"FROM " +
"POB_CODE " +
"WHERE " +
"DISPLAY_SORT_ORDER >=1 " +
"AND " +
"DISPLAY_SORT_ORDER <=60",
null
)
.ToList();

编辑,更正了 SQL

将它们添加到字典时不需要转换它们。您要做的是像往常一样填充字典(使用键和值选择器),然后在序列化之前,将其更改为您的类型列表,然后定义如何序列化它。

试试吧:

[Obsolete]
public string POB_CODE()
{
Dictionary<string, string> _dicts = null;
try
{
using (OracleConnection Oconn = new OracleConnection(connectionORI))
{
_dicts = Oconn.Query<KeyValuePair<string, string>>(
"SELECT " +
"POB_CODE AS Key," +
"POB_DESC AS Value " +
"FROM " +
"POB_CODE " +
"WHERE " +
"DISPLAY_SORT_ORDER >=1 " +
"AND " +
"DISPLAY_SORT_ORDER <=60",
null
)
.ToDictionary(p => p.Key, p => p.Value);
}
}
catch (SqlException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
jsonOutputStyle[] styledDictionary = _dicts.Select(p => new jsonOutputStyle() { Name = p.Key, Value = p.Value }).ToArray();
return JsonConvert.SerializeObject(styledDictionary, Formatting.None);
}

重要变化的细分:

.ToDictionary(p => p.Key, p => p.Value);

而不是

.ToDictionary(pair => new jsonOutputStyle() { 
Name = pair.Key, 
Value = pair.Value
});

在这里,我们首先需要得到一个简单的字典,其中string作为键,string作为值。我们通过使用两个选择器来做到这一点。一个用于键 (p => p.Key),一个用于值 (p => p.Value)。我们还不需要担心序列化。

现在对于序列化,我们不直接序列化字典,而是将字典强制转换为元组类型的数组。这允许我们序列化您在该类中定义的名称,而不是预定义的属性名称。

jsonOutputStyle[] styledDictionary = _dicts.Select(p => new jsonOutputStyle() { Name = p.Key, Value = p.Value }).ToArray();
return JsonConvert.SerializeObject(styledDictionary, Formatting.None);

同样在这里是一个非常相似的问题的答案,它基本上做了我刚刚向您展示的相同的事情。

也对于任何想知道当您序列化YourTupel[]vsDictionary<string, string>vsList<KeyValuePair<string, string>>时会发生什么的人:

Serialized ArrayOfYourType:
[{"Name":"Key 0","Value":"Value 0"},{"Name":"Key 1","Value":"Value 1"}]
Serialized Dictionary:
{"Key 0":"Value 0","Key 1":"Value 1"}
Serialized List:
[{"Key":"Key 0","Value":"Value 0"},{"Key":"Key 1","Value":"Value 1"}]

编辑:
我假设你需要字典(例如检查键是否不同或类似的东西)。如果您根本不需要字典,则可以直接强制转换数组,而无需对字典执行某些操作。

代码将如下所示:
Ps。这个答案中的编辑使用更漂亮的功能来使其更加干净,去看看吧。

[Obsolete]
public string POB_CODE()
{
jsonOutputStyle[] styledDictionary = null;
try
{
using (OracleConnection Oconn = new OracleConnection(connectionORI))
{
_dicts = Oconn.Query<KeyValuePair<string, string>>(
"SELECT " +
"POB_CODE AS Key," +
"POB_DESC AS Value " +
"FROM " +
"POB_CODE " +
"WHERE " +
"DISPLAY_SORT_ORDER >=1 " +
"AND " +
"DISPLAY_SORT_ORDER <=60",
null
)
.Select(p => new jsonOutputStyle() { Name = p.Key, Value = p.Value }).ToArray();
}
}
catch (SqlException ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return JsonConvert.SerializeObject(styledDictionary, Formatting.None);
}

最新更新