WCF返回xml而不是JSON

时间:2014-05-22 12:01:49

标签: asp.net xml json wcf

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

我期待JSON out put bu无法得到它,

[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;
    }

此函数返回输出,如

{"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还是什么?我不想要这么做,所以马上将其转换为JSON,如

[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;

        }

它提供正确的输出,如

[{"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}]

任何人都可以帮我解决我的第一个网络服务方法有什么问题。 感谢您阅读这么久的问题。

2 个答案:

答案 0 :(得分:1)

你的问题中缺少的主要内容是DailyBingRTT_Performance_Last7Days_Result

是自定义构建类还是数据表?结构是什么?

解决方案如下:

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

List<Performance> ... 

return serializer.Serialize(rows);

答案 1 :(得分:1)

不要从Web服务返回DataTable。你需要通过电线传输的负载不必要地更重。

DataTable对象不仅包含数据,还包含架构信息。此信息不仅对非Microsoft客户端无用,而且此架构信息也会增加要传输的数据的大小。

而是创建一个DTO(数据传输对象),而不是将这些集合发送到客户端。