我有一个 WCF 服务,我正在构建一个名为的对象FinalList
,其中包含一个名为 Chart 的对象和一个List<Data>
. 我想以以下格式返回我的 ajax 函数 json 数据:
{"d":{"chart":{"caption":"Year","exportatclient":"1","exportenabled":"1","exporthandler":"chartExporter","labelDisplay":"wrap" ,"numberprefix":"","showborder":"1","slantLabels":"1","useroundedges":null,"yaxisname":"点击数"},"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}]}}
但是我的服务返回是这样的:
{"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":"点击数"},"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}]}}
当我使用 WebServices (asmx) 时,我只需要将我的类声明为“对象”而不是 FinalList,那么结果就是我需要的正确 json 格式。
我已经尝试将它转换为字符串,它给我带来了正确的东西,但包括 '\' (例如 {"d":{\"chart\":{\"caption\":\"Year\") 这不是什么我想。
有谁知道我如何删除这些“__type”和类名?
我正在使用.Net 4
我的 FinalList 类如下:
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; }
}
}
我的服务合同包含
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
在其声明之上。