Reputation: 1377
I have a WCF service and I am building an object called FinalList
that contains an Object called Chart and a List<Data>
.
I want to return to my ajax function json data in the format below:
{"d":{"chart":{"caption":"Year","exportatclient":"1","exportenabled":"1","exporthandler":"chartExporter","labelDisplay":"wrap","numberprefix":"","showborder":"1","slantLabels":"1","useroundedges":null,"yaxisname":"Number of hits"},"data":[{"label":"1960s","link":null,"tooltext":null,"value":3},{"label":"1970s","link":null,"tooltext":null,"value":56},{"label":"1980s","link":null,"tooltext":null,"value":98}]}}
However my Service is returning like this:
{"d":{"__type":"FinalList:#MyNamespace.FusionChartsFields","chart":{"__type":"Chart:#MyNamespace.FusionChartsFields","caption":"Year","exportatclient":"1","exportenabled":"1","exporthandler":"chartExporter","labelDisplay":"wrap","numberprefix":"","showborder":"1","slantLabels":"1","useroundedges":null,"yaxisname":"Number of hits"},"data":[{"__type":"Data:#MyNamespace.FusionChartsFields","label":"1960s","link":null,"tooltext":null,"value":3},{"__type":"Data:#MyNamespace.FusionChartsFields","label":"1970s","link":null,"tooltext":null,"value":56},{"__type":"Data:#MyNamespace.FusionChartsFields","label":"1980s","link":null,"tooltext":null,"value":98}]}}
When I used WebServices (asmx) I only needed to declare my class as "object" instead of FinalList, then the results were in the correct json format I need.
I have tried converting it into string and it brings me the correct thing but including '\' (e.g. {"d":{\"chart\":{\"caption\":\"Year\") and that's not what I want.
Does anyone know how I can remove these "__type" and class name?
I am using .Net 4
My FinalList class is as follows:
public class FinalList
{
private Chart _chart;
private List<Data> _data = new List<Data>();
public Chart chart
{
get { return _chart; }
set { _chart = value; }
}
public List<Data> data
{
get { return _data; }
set { _data = value; }
}
}
My service contract contains
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
above its declaration.
Upvotes: 1
Views: 5586
Reputation: 836
Try adding a constructor as below:
public class FinalList
{
private Chart _chart;
private List<Data> _data = new List<Data>();
public Chart chart
{
get { return _chart; }
set { _chart = value; }
}
public List<Data> data
{
get { return _data; }
set { _data = value; }
}
protected internal FinalList() { }
}
Upvotes: 0
Reputation: 747
What behaviour are you using for your WCF service? If you are using <enableWebScript />
then switching to <webHttp/>
in the service behaviour configuration will solve your problem.
As Cybermaxs said, if you are using an ASP.NET Ajax client then this won't be a problem and you may continue to use <enableWebScript />
.
Upvotes: 1
Reputation: 24556
Basically the result is wrapped in an additional type (presumably to allow for multiple results?) and there's a __type property attached to each object returned. Even simple result values at least include the wrapper type, so even say a string result includes the d: type.
If you're using MS AJAX on the client there's no issue - the client knows how to parse this JSON into a clean object. But if you're using some other mechanism - jQuery or Prototype for example to retrieve a result set you get back a funky object where you have to go result.d.LastName for example, which is ugly.
Try this,
[OperationContract]
[WebInvoke(BodyStyle=WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Upvotes: 1