I'm providing a WebApi 2 endpoint that is done in this way:
My controller is simply:
public IDictionary<MyClass, int> GetMyClasses(string id)
{
Dictionary<MyClasses, int> sample = new Dictionary<MyClasses, int>();
sample.Add(new MyClasses()
{
Property1 = "aaa",
Property2 = 5,
Property3 = 8
},10);
return sample;
}
The structure of MyClass is:
public class MyClass
{
string Property1 {get;set;}
int Property2 {get;set;}
int Property3 {get;set;}
}
When I run my webservice, the helper webpage shows me that the expected outputs are:
{ "MyNamespace.MyProject.MyClass": 1 }
On the other hand the xml sample is what I'd like (except that I want the json, not the xml):
<ArrayOfKeyValueOfMyClassintl85fHlC_P xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<KeyValueOfMyClassintl85fHlC_P>
<Key xmlns:d3p1="http://schemas.datacontract.org/2004/07/MyNamespace.MyProject.MyClass">
<d3p1:Property1>sample string 4</d3p1:Property1>
<d3p1:Property2>8</d3p1:Property2>
<d3p1:Property3>5</d3p1:Property3>
</Key>
<Value>1</Value>
</KeyValueOfMyClassintl85fHlC_P>
</ArrayOfKeyValueOfMyClassintl85fHlC_P >
I also ran the endpoint with Postman and it confirms that the returned value is the one previewed by the WebApi out of the box page.
Why the json is "wrong" and the xml is well done (I mean that contains all the data)?
UPDATED:
I expected MyClass serialized in json like this:
{
"Property1": "sample string 4",
"Property2": 8,
"Property3": 5
}
This should be the structure of the key of my dictionary, as it is in the xml representation.
Thanks
This is kind of hacky, but I had success by converting the Dictionary to a List object before running it through JsonConvert. Check it out:
Whereas . . .
Hope that helps.
What does your Controller look like? The endpoint should look something like this:
If you're still having JSON serialization issues after that, you can configure the default
JsonFormatter
type in Web API:GlobalConfiguration.Configuration.Formatters.JsonFormatter;
. See the ASP.NET Web API Serialization Documentation for more information.Typically dictionaries are used to produce more dynamic JSON objects, by using the Key/Value pairs as name/value pairs on a JavaScript object. But a JSON object cannot use another JSON object as its key. For example, the following syntax is not valid:
So you'll need to decide exactly how you want this information represented in JSON. Are you thinking of it as an array of Key/Value objects?
In that case, return a
List<KeyValuePair<string, int>>
from your method, viadict.ToList()
.Do your keys and values have meanings? Perhaps you should create a class to represent each item with custom property names, via
dict.Select(kvp => new MyDto { MyClass = kvp.Key, Foo = kvp.Value }).ToList()
:Do you want to stick with objects, but have the left-hand side be a string representation of your class? You can do this by implementing the
ToString()
method on MyClass: