如何使用WCF Web Api返回Json值

时间:2011-08-31 06:21:27

标签: json wcf-web-api

我已经创建了一个示例WCF休息模板WebApi,我已经使用Entity Framework获取数据,当我运行服务时,它返回显示结果的字符串值,但在json值的末尾添加XML代码,如下面我该如何解决这个问题。

[{"AccountId":1,
  "AccountNumber":"AC001",
  "AccountType":"Restaurant",
  "BusinessName"‌​:"Red SpiceInc",
  "PrimaryContactFirstName":"Varma",
  "PrimaryContactLastName":"Bhupatiraju",
  "P‌​rimaryContactPhone":"(949) 374 2114",
  "PrimaryContactEmail":"redspice@mybusinessapp.com",
  "AccountGuid":"918D3E66-CEFE-11E0-8C2F-0C0B4824019B",
  "EntityState":1,"EntityKey":null}]
<?xml version="1.0" encoding="utf-8"?><Stream p1:nil="true" xmlns:p1="w3.org/2001/XMLSchema-instance"; />

我的代码

[WebGet(UriTemplate = "GetSetting({LocationGuid},{settingName})", ResponseFormat = WebMessageFormat.Json)]
public Stream GetSetting(string LocationGuid, string settingName)
{
    string str = string.Empty;
    string strJSON = string.Empty;
    dynamic contactResponse = new JsonObject();
    List<setting> Result;
    Result = new List<setting>();
    var Location = from acc in objEntity.locations where acc.LocationGuid == LocationGuid select acc;
    if (Location.Count() > 0)
    {
        var LocationId = (from acc in objEntity.locations where acc.LocationGuid == LocationGuid select acc).First();
        var objSetting = from cat in objEntity.settings where cat.SettingName == settingName & cat.LocationId == LocationId.LocationId select cat;
        setting SettingList = new setting();
        foreach (setting setting in objSetting)
        {
            setting Settinglist = new setting();
            Settinglist.SettingId = setting.SettingId;
            Settinglist.AccountId = setting.AccountId;
            Settinglist.LocationId = setting.LocationId;
            Settinglist.SettingName = setting.SettingName;
            Settinglist.SettingValue = setting.SettingValue;
            Settinglist.FieldType = setting.FieldType;
            Result.Add(Settinglist);
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        strJSON = js.Serialize(Result);
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
        return new MemoryStream(Encoding.UTF8.GetBytes(strJSON));
    }
    else
    {
        return null;
    }
}

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

我相信回归POCO就足够了 方法签名是

public List<setting> GetSetting(string, string)

WCF Web API将根据您的请求标头(接受:application / json或application / xml)将对象序列化为json或xml

答案 1 :(得分:0)

正如misaxi的回应中所提到的,WebApi WebGet操作通常不需要关心如何返回响应。 Web操作的职责是简单地返回数据。 E.g ....

[WebGet(UriTemplate = "GetSetting({LocationGuid},{settingName})"]
public List<setting> GetSetting(string LocationGuid, string settingName)
{
    List<setting> Result = null;
    var Location = from acc in objEntity.locations where acc.LocationGuid == LocationGuid select acc;
    if (Location.Count() > 0)
    {
        Result = new List<setting>();
        ....
        foreach (setting setting in objSetting)
        {
            ....
            Result.Add(Settinglist);
        }
     }
     return Result;
}

客户端接收XML,JSON,JSONP,HTML等这一事实取决于客户端(与服务器端支持相结合)。客户端的请求标头将包含类似Accept: application/jsonAccept: application/xml的内容或客户端所遵循的任何表示形式。 WebApi预装了一些标准格式,XML(默认)和Json。您可以根据需要引入更多格式以接受更多样化的Accept: ....标头集,但是从您的数据到这些请求的格式的转换通常超出了您的Web操作的范围,并依赖于您设置的格式化程序(或默认设置)初始化服务时。

同样,WebPost操作通常不关心请求的主体是XML还是JSON,在调用操作时,格式化程序已经将该有效负载转换为方法的参数。