.ToDictionary KeyValuePair 将 key/value 重命名为 Name/Value json.net 输出

.ToDictionary KeyValuePair rename key/value to Name/Value json.net output

嘿,我有以下代码,我正在尝试重命名 "KEY""VALUE"[= 的默认值31=] 到 "Name""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);
}

产生以下错误:

Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.Dictionary<WCF.Service.NNicsAPI.jsonOutputStyle, System.Collections.Generic.KeyValuePair<string, string>>' to '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);

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

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

_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

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

试一试:

[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)。我们还不需要担心序列化。

现在为了序列化,我们不直接序列化字典,而是将字典转换为您的 tupel-type 的数组。这允许我们序列化在 class 中定义的名称 you 而不是 pre-defined property-names.

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

另外 right here 是对一个非常相似的问题的答案,该问题基本上与我刚刚向您展示的内容相同。

还有想知道序列化 YourTupel[] vs Dictionary<string, string> vs List<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);
}