【问题标题】:WebInvoke return Json string with extra quotesWebInvoke 返回带有额外引号的 Json 字符串
【发布时间】:2023-03-23 21:30:01
【问题描述】:

我正在开发 WCF 网络服务。 我需要创建一个返回 Json 字符串的 Post 服务,服务声明如下:

[WebInvoke(UriTemplate = "GetMatAnalysis",  ResponseFormat = WebMessageFormat.Json, 
                                                RequestFormat = WebMessageFormat.Json, 
                                                BodyStyle = WebMessageBodyStyle.WrappedRequest, 
                                                Method = "POST")]
string GetMatAnalysis(Stream image);

在此消息中,我使用JavaScriptSerializer().Serialize() 序列化对象 然后返回。

但是,当我得到响应时,在字符串的开头和结尾有一个额外的双引号。例如我得到:"{"results" : 10 }" 而不是 {"results" : 10 }

我尝试将返回类型更改为System.ServiceModel.Channels.Message 我收到此错误:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior contract: http://tempuri.org/:IMyWebServices ----> System.InvalidOperationException: The operation 'GetMatAnalysis' could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types. When using System.ServiceModel.Channels.Message or types with MessageContractAttribute, the method must not use any other types of parameters.

如何让它返回一个不带双引号的 json 字符串?

其他信息:

当我像这样使用 GET 请求时:

[OperationContract(Name = "Messages")]
[WebGet(UriTemplate = "Messages/GetMessage", ResponseFormat = WebMessageFormat.Json)]
Message GetAdvertisment();

返回类型是消息,它工作正常。收到的Json字符串是正确的。

非常感谢任何帮助。 谢谢

【问题讨论】:

  • 我认为这是 MS 的一个恼人的怪癖。他们再次使用自己的扭曲逻辑应用标准
  • @L.B 谢谢它的工作。把它写成接受它的答案

标签: c# json wcf post


【解决方案1】:

由于ResponseFormat = WebMessageFormat.Json,WCF 服务将您返回的对象序列化为 Json。您还可以使用JavaScriptSerializer().Serialize() 并获得双重序列化。

【讨论】:

    【解决方案2】:

    返回 Stream 而不是字符串,并将传出响应的内容类型设置为“application/json; chatset=utf-8”。这将正确返回所有内容。

    界面:

    [ServiceContract]
    interface ITestService
    {        
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetMatAnalysis")]
        Stream GetMatAnalysis(Stream image);
    }
    

    服务:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class TestService : ITestService
    {
        public Stream GetMatAnalysis(Stream image)
        {
            MatAnalysisResult matAnalysis = new MatAnalysisResult { results = 10 };
    
            string result = JsonConvert.SerializeObject(matAnalysis);
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
    
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
    }
    

    结果将是:

    {"results":10}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2018-03-14
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多