我想构建自己的JSON,并让服务返回一个字符串,这里是代码
[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;
}
Run Code Online (Sandbox Code Playgroud)
我得到的响应包含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}"
Run Code Online (Sandbox Code Playgroud)
值正确编码,但json本身格式不正确.这些导致它走出了怪癖.
如何在没有前面的字符串的情况下返回一个字符串?
Ole*_*leg 110
目前您的Web方法与... String一起返回ResponseFormat = WebMessageFormat.Json.它遵循字符串的JSON编码.对应于www.json.org.字符串中的所有双引号都将使用反斜杠进行转义.所以你目前有两个JSON编码.
返回任何类型数据的最简单方法是将GetCurrentCart()Web方法的输出类型更改为Stream或Message(从System.ServiceModel.Channels)而不是String.
见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和http://msdn.microsoft.com/en-us/library/cc681221(VS.90).aspx代码示例.
因为您没有在您的问题中写入您使用的.NET版本,我建议您使用通用且最简单的方法:
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));
}
Run Code Online (Sandbox Code Playgroud)
我尝试了Oleg建议的方法,但发现当我使用此方法处理大量数据时,在JSON字符串的末尾附加了空关键字.
示例:对于包含大量数据的json {"JsonExample":"xxxxx"} null
找到了解决这个问题的解决方案:http: //wcf.codeplex.com/workitem/67写了下面的函数,它将接受一个对象并返回一个Pure Json输出.所以在主方法中返回我的对象之前,我调用了下面的方法.
public HttpResponseMessage ReturnPureJson(object responseModel)
{
HttpResponseMessage response = new HttpResponseMessage();
string jsonClient = Json.Encode(responseModel);
byte[] resultBytes = Encoding.UTF8.GetBytes(jsonClient);
response.Content = new StreamContent(new MemoryStream(resultBytes));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
return response;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62262 次 |
| 最近记录: |