在wcf中返回原始json(字符串) [英] Returning raw json (string) in wcf

查看:160
本文介绍了在wcf中返回原始json(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建自己的JSON,并让该服务返回一个字符串,这是代码

I want to build my own JSON, and have the service return a string, here is the code

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
public string GetCurrentCart()
{
    //Code ommited
    string jsonClient = null;
    var j = new { Content = response.Content, Display=response.Display, SubTotal=response.SubTotal};
    var s = new JavaScriptSerializer();
    jsonClient = s.Serialize(j);
    return jsonClient;
}

我得到的响应包含用于在c#中的字符串中创建\的\.

The response I am getting contains the \" used to create "'s in strings in c#.

以下是响应.

"{\"Content\":\"\\r\\n\\u003cdiv\\u003e\\r\\n\\u003cinput type=\\\"hidden\\\" name=\\\"__VIEWSTATE\\\" id=\\\"__VIEWSTATE\\\" value=\\\"\/wEPDwUBMA9kFgJmD2QWAmYPZBYGAgMPFgIeBFRleHQFKFlvdSBoYXZlIG5vIGl0ZW1zIGluIHlvdXIgc2hvcHBpbmcgY2FydC5kAgUPFgIeB1Zpc2libGVoZAIHDxQrAAIPFgIfAWhkZGQYAQUMY3RsMDEkbHZDYXJ0D2dkoWijqBUJaUxmDgFrkGdWUM0mLpgQmTOe8R8hc8bZco4=\\\" \/\\u003e\\r\\n\\u003c\/div\\u003e\\r\\n\\r\\n\\u003cdiv class=\\\"block block-shoppingcart\\\"\\u003e\\r\\n    \\u003cdiv class=\\\"title\\\"\\u003e\\r\\n        \\u003cspan\\u003eShopping Cart\\u003c\/span\\u003e\\r\\n    \\u003c\/div\\u003e\\r\\n    \\u003cdiv class=\\\"clear\\\"\\u003e\\r\\n    \\u003c\/div\\u003e\\r\\n    \\u003cdiv class=\\\"listbox\\\"\\u003e\\r\\n        You have no items in your shopping cart.\\r\\n        \\r\\n        \\r\\n    \\u003c\/div\\u003e\\r\\n\\u003c\/div\\u003e\\r\\n\",\"Display\":\"You have no items in your shopping cart.\",\"SubTotal\":null}"

值已正确编码,但json本身格式不正确.这些\会使它变得毫无用处.

The values are being correctly encoded, but the json itself is not properly formatted. These \'s cause it to go out of wack.

如何返回一个字符串,而在'的前面没有\呢?

How do I return a string without the \'s in front of the "'s?

推荐答案

当前,您的Web方法将StringResponseFormat = WebMessageFormat.Json一起返回.它遵循字符串的JSON编码.对应于www.json.org,字符串中的所有双引号都将使用反斜杠转义.因此,您目前具有双重JSON编码.

Currently your web method return a String together with ResponseFormat = WebMessageFormat.Json. It follow to the JSON encoding of the string. Corresponds to www.json.org all double quotes in the string will be escaped using backslash. So you have currently double JSON encoding.

返回任何类型数据的最简单方法是将GetCurrentCart() Web方法的输出类型更改为StreamMessage(从System.ServiceModel.Channels)而不是String.
参见 http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx

The easiest way to return any kind of data is to change the output type of GetCurrentCart() web method to Stream or Message (from System.ServiceModel.Channels) instead of String.
See http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx, http://msdn.microsoft.com/en-us/library/ms789010.aspx and http://msdn.microsoft.com/en-us/library/cc681221(VS.90).aspx for code examples.

因为您没有在问题中写下使用的.NET版本,所以建议您使用通用且最简单的方法:

Because you don't wrote in your question which version of .NET you use, I suggest you to use an universal and the easiest way:

public Stream GetCurrentCart()
{
    //Code ommited
    var j = new { Content = response.Content, Display=response.Display,
                  SubTotal=response.SubTotal};
    var s = new JavaScriptSerializer();
    string jsonClient = s.Serialize(j);
    WebOperationContext.Current.OutgoingResponse.ContentType =
        "application/json; charset=utf-8";
    return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));
}

这篇关于在wcf中返回原始json(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆