Make a web api serialize a dictionary with a key value as a key using the data attribute

I have a dictionary that I am serializing with the json.net serializer, and it currently produces

{"phrases":[{"Key":"my-key1","Value":"blah"},{"Key":"my-key2","Value":"blah2"}]}

however I want it to output

{"phrases":["my-key1":"blah"},{"my-key2":"blah2"}]}

my model looks like

public class Phrases
{
    public Dictionary<string, string> phrases;
}

Is there a data attribute that I can apply to the phrase model to make this happen?

I found the following, but don't want to return the Serialize string to the keyword dictionary using Json.Net?

UPDATE:

I am extending the web api controller as follows, if I use JsonConvert.SerializeObject (), I get the correct serialization, however I will then have a string to return.

public class PhraseController : ApiController
    {
        private IApplicationModel applicationModel;

        public Phrases Get(string id)
        {
            var locale = new CultureInfo(id).LCID;
            var phrases = applicationModel.Phrases.Where(x => x.Locale = locale).ToDictionary(x => x.Name, y => y.Value);

            return new Phrases() { phrases = phrases };
        }

        public PhraseController(IApplicationModel applicationModel)
        {
            this.applicationModel = applicationModel;
        }
    }
+5
source share
1 answer

.

, DataContractJsonSerializer. , , JsonNetFormatter, .

GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.JsonFormatter);

GlobalConfiguration.Configuration.Formatters.Add(new JsonNetFormatter(null));

( -, - DataContractJsonSerializer)

+2

All Articles