Search code examples
c#json.netoracle12cdapperkeyvaluepair

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


Hey all I have the following code that I am trying to rename the default values of "KEY" and "VALUE" to "Name" and "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);
}

Which produces the error of:

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>'

So from that I'm not sure what all I need to do in order to correct the issue so that the json output looks like this:

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

and not like this:

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

Solution

  • Try this:

    _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);
    

    I don't understand why you want "Key" to be named as "Name". When a dictionary is converted into JSON it will look like this {"actual_value_of_key" : "value"}. You will not see the variable Name written.

    EDIT: if you want JSON output like [{"Name":"","Value":""},{"Name":"Female","Value":"F"},{"Name":"Male","Value":"M"}] then don't use the dictionary. Use your defined 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();
    

    Edit, corrected SQL