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

时间:2019-06-18 15:52:52

标签: c# json.net oracle12c dapper keyvaluepair

嘿,我所有下面的代码都试图将“ 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"}];

2 个答案:

答案 0 :(得分:2)

尝试一下:

_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

答案 1 :(得分:1)

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

尝试一下:

[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类型的数组。这使我们可以序列化在该类中定义的名称 you 而不是预定义的属性名称。

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

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

对于任何想知道在序列化YourTupel[]Dictionary<string, string>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"}]

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

代码如下:
附言this answer中的编辑使用dapper功能使其更加清晰,请检查一下。

[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);
}