WCF返回XML而不是JSON

WCF返回XML而不是JSON

问题描述:

我消费从其他项目中的Web服务在我的客户端项目(虽然这两个项目都在同一个解决方案)

I consuming web-service from other project in my client project (ALTHOUGH both project are in same solution)

我期待JSON出来放BU无法得到它,

I am expecting JSON out put bu unable to get it,

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "FetchSitePerformanceDT/{fromDate}/{country}")]
    public DataTable FetchSitePerformanceDT(string fromDate, string country)
    {
        SitePerformance objSiteP = new SitePerformance();
        DataTable dt = new DataTable();
        dt = objSiteP.getPerformanceByDateAndCountryAsDataTable(fromDate, country);
        return dt;
    }

这个函数返回的输出一样,

This function returns output like,

{"FetchSitePerformanceDTResult":"<DataTable xmlns=\"http:\/\/schemas.datacontract.org\/2004\/07\/System.Data\"><xs:schema id=\"NewDataSet\" xmlns:xs=\"http:\/\/www.w3.org\/2001\/XMLSchema\" xmlns=\"\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"DailyBingRTT_Performance_Last7Days_Result\" msdata:UseCurrentLocale=\"true\"><xs:complexType><xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\"><xs:element name=\"DailyBingRTT_Performance_Last7Days_Result\"><xs:complexType><xs:sequence><xs:element name=\"DailyTimeStamp\" type=\"xs:string\" minOccurs=\"0\"\/><xs:element name=\"Performance\" type=\"xs:string\" minOccurs=\"0\"\/><\/xs:sequence><\/xs:complexType><\/xs:element><\/xs:choice><\/xs:complexType><\/xs:element><\/xs:schema><diffgr:diffgram xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><DocumentElement xmlns=\"\"><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result1\" msdata:rowOrder=\"0\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 03, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result2\" msdata:rowOrder=\"1\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 04, 2013<\/DailyTimeStamp><Performance>119542<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result3\" msdata:rowOrder=\"2\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 05, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result4\" msdata:rowOrder=\"3\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 06, 2013<\/DailyTimeStamp><Performance>119542<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result5\" msdata:rowOrder=\"4\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 07, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result6\" msdata:rowOrder=\"5\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 08, 2013<\/DailyTimeStamp><Performance>119542<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result7\" msdata:rowOrder=\"6\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 09, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><\/DocumentElement><\/diffgr:diffgram><\/DataTable>"}

那是什么? XML还是什么?我不想taht,所以manualy它转换成JSON一样,

What is that? xml or what? I don't want taht, so manualy converted it to JSON like,

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "FetchSitePerformanceStream/{fromDate}/{country}")]
        public Stream FetchSitePerformanceStream(string fromDate, string country)
        {
            SitePerformance objSiteP = new SitePerformance();

            List<DailyBingRTT_Performance_Last7Days_Result> l = new List<DailyBingRTT_Performance_Last7Days_Result>();
            l = objSiteP.getPerformanceByDateAndCountry(fromDate, country);

            var javaScriptSerializer = new JavaScriptSerializer();
            var json = Encoding.UTF8.GetBytes(javaScriptSerializer.Serialize(l));
            var memoryStream = new MemoryStream(json);
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return memoryStream;

        }

它给出​​正确的输出一样,

It gives correct output like,

[{"DailyTimeStamp":"Nov 03, 2013","Performance":106917},{"DailyTimeStamp":"Nov 04, 2013","Performance":119542},{"DailyTimeStamp":"Nov 05, 2013","Performance":106917},{"DailyTimeStamp":"Nov 06, 2013","Performance":119542},{"DailyTimeStamp":"Nov 07, 2013","Performance":106917},{"DailyTimeStamp":"Nov 08, 2013","Performance":119542},{"DailyTimeStamp":"Nov 09, 2013","Performance":106917}]

任何人都可以请帮我什么是错在我的Web服务的第一个方法。
感谢您阅读这么长的问题。

Can anyone please help me what is wrong in my first method of web-service. Thanks for reading so long question.

要在你的问题缺少的主要事情是DailyBingRTT_Performance_Last7Days_Result

The main thing to missing in your question is DailyBingRTT_Performance_Last7Days_Result

是自定义生成类或数据表?什么是结构?

Is that custom build class or data table ? What is the structure?

该溶液可以是如下:

Class Performance { 
  publc datetime DailyTimeStamp { get; set;}
  public int performance ... 
}

List<Performance> ... 

return serializer.Serialize(rows);