Xml Can';无法从WCF服务中获取JSON

Xml Can';无法从WCF服务中获取JSON,xml,wcf,json,web-services,soap,Xml,Wcf,Json,Web Services,Soap,我似乎无法成功地从WCF服务中获取JSON,即使我使用以下属性标记该方法: [WebGet(UriTemplate = "Product/{productIdString}", ResponseFormat = WebMessageFormat.Json)] 或 我总是得到XML,不管我是作为数据集还是列表返回它 唯一有效的方法是手动将JSON作为字符串返回,但它也封装在XML中 任何线索?[WebGet](和[WebInvoke])属性仅由WebHttpBehavior(如果您使用的是配置)

我似乎无法成功地从WCF服务中获取JSON,即使我使用以下属性标记该方法:

[WebGet(UriTemplate = "Product/{productIdString}",
ResponseFormat = WebMessageFormat.Json)]

我总是得到XML,不管我是作为
数据集
还是
列表
返回它

唯一有效的方法是手动将JSON作为字符串返回,但它也封装在XML中


任何线索?

[WebGet]
(和
[WebInvoke]
)属性仅由
WebHttpBehavior
如果您使用的是配置)识别。请确保正在命中的端点设置了该行为。

尝试以下操作:

[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "products")]
    public IList<JxProduct> GetProductList()
    {
        List<JxProduct> products = new List<JxProduct>();
        products.Add(new JxProduct { Description = "Tire", Id = 1, Price = 39.99});
        products.Add(new JxProduct { Description = "Tube", Id = 2, Price = 4.99 });
        products.Add(new JxProduct { Description = "Patch", Id = 3, Price = 3.99});

        return products;
    }
[WebInvoke(Method=“GET”,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,
UriTemplate=“产品”)]
公共IList GetProductList()
{
列表产品=新列表();
products.Add(新JxProduct{Description=“Tire”,Id=1,Price=39.99});
products.Add(新的JxProduct{Description=“Tube”,Id=2,Price=4.99});
products.Add(新的JxProduct{Description=“Patch”,Id=3,Price=3.99});
退货产品;
}
您还可以查看下面的文章,其中详细介绍了web.config设置。

您的服务使用什么绑定??JSON几乎只在
webHttpBinding
(REST风格的WCF服务)上起作用
[WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "products")]
    public IList<JxProduct> GetProductList()
    {
        List<JxProduct> products = new List<JxProduct>();
        products.Add(new JxProduct { Description = "Tire", Id = 1, Price = 39.99});
        products.Add(new JxProduct { Description = "Tube", Id = 2, Price = 4.99 });
        products.Add(new JxProduct { Description = "Patch", Id = 3, Price = 3.99});

        return products;
    }