我如何从C#返回JSON,但删除响应D:null

发布于 2025-02-02 04:16:59 字数 1752 浏览 2 评论 0原文

如何从我的服务响应中删除d:null? 我尝试了许多选项,它继续返回值d:null,我不知道该怎么做来解决问题,

我添加了我的c#代码,我的web.config的配置以及服务的响应,您可以看到正确的响应:200,但它继续添加值D:null

[WebMethod]
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

        public void test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;


            Context.Response.Write(js.Serialize(des));
        }


<system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="DebugJSonBehavior" >
                    <enableWebScript />
                    <!--need set automaticFormatSelectionEnabled attribute -->
                    <webHttp automaticFormatSelectionEnabled="true" />
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="DebugJSonBehavior" >
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

    </system.serviceModel>

这是我调用Postman服务的响应 { “响应”:“ 200” } { “ D”:空 }

谢谢

how can I remove d:null from my service response?
I have tried many options and it continues to return the value d:null and I don't know what else can be done to solve the problem

I add my c# code, the configuration of my web.config and the response of the service, where you can see the correct response response:200 but it continues adding the value d:null

[WebMethod]
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

        public void test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;


            Context.Response.Write(js.Serialize(des));
        }


<system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="DebugJSonBehavior" >
                    <enableWebScript />
                    <!--need set automaticFormatSelectionEnabled attribute -->
                    <webHttp automaticFormatSelectionEnabled="true" />
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="DebugJSonBehavior" >
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

    </system.serviceModel>

this is my response when i call the service from postman
{
"response": "200"
}{
"d": null
}

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聚集的泪 2025-02-09 04:16:59

这是我的解决方案

        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void test(string number)
        {
          
        
            WebException webException = null;

            var js = new System.Web.Script.Serialization.JavaScriptSerializer();
            Resultres = new Result();
            res.response = "ok";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            HttpContext.Current.Response.Write(js.Serialize(res));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }

this was my solution

        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void test(string number)
        {
          
        
            WebException webException = null;

            var js = new System.Web.Script.Serialization.JavaScriptSerializer();
            Resultres = new Result();
            res.response = "ok";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            HttpContext.Current.Response.Write(js.Serialize(res));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
作业与我同在 2025-02-09 04:16:59

使用此代码,它将返回我在映像中看到的内容,其中d:null在响应末尾添加了postman

 [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
       
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;

            Response res = new Response();
            res.response = "Apple";
            string json = JsonConvert.SerializeObject(res);
            Context.Response.Write(json);
        }

“在此处输入映像说明”

现在返回一个字符串邮递员在响应中包括d,而我不don' t知道如何删除D:

 [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public string test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
       
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;

            Response res = new Response();
            res.response = "Apple";
            string json = JsonConvert.SerializeObject(res);
            return json;
            //Context.Response.Write(json);
        }

响应邮递员

With this code it returns me what you see in the image where d:null is added at the end of the response in postman

 [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
       
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;

            Response res = new Response();
            res.response = "Apple";
            string json = JsonConvert.SerializeObject(res);
            Context.Response.Write(json);
        }

enter image description here

now returning a string postman keeps including the d in the response and I don't know how to remove d:

 [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public string test(string numberDoc)
        {
            if (!inicializo) inicializa();
            string resp = "200";
            WebException webException = null;
       
            Context.Response.Clear();
            // Context.Response.ContentType = "application/json";
            Response des = new Response();
            des.response = resp;

            Response res = new Response();
            res.response = "Apple";
            string json = JsonConvert.SerializeObject(res);
            return json;
            //Context.Response.Write(json);
        }

response postman
enter image description here

何时共饮酒 2025-02-09 04:16:59
[WebMethod]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]

    public void Locations(string Location)
    {
        JavaScriptSerializer Js = new JavaScriptSerializer();
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        
        Query = "Select xxxx";
        DataTable Dt = Func.GetDataTable(Query);

        if (Dt.Rows.Count == 0)
        {
            LocationsCs Data = new LocationsCs();
            Data.Id = "0";
            Data.Location = "0";
            Context.Response.Write(Js.Serialize(Data));
        }
        else
        {
            List<LocationsCs> Locations = new List<LocationsCs>();
            for (int i = 0; i < Dt.Rows.Count; i++)
            {
                Locations.Add(new LocationsCs
                {
                    Id = Dt.Rows[i]["Id"].ToString(),
                    Location = Dt.Rows[i]["Location"].ToString(),
                });
            }
            //string  strResponse = Js.Serialize(Data);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            HttpContext.Current.Response.Write(Js.Serialize(Locations));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
    }

    public class LocationsCs
    {
        public String Id;
        public String Location;
    }

这对我有用。

[WebMethod]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]

    public void Locations(string Location)
    {
        JavaScriptSerializer Js = new JavaScriptSerializer();
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        
        Query = "Select xxxx";
        DataTable Dt = Func.GetDataTable(Query);

        if (Dt.Rows.Count == 0)
        {
            LocationsCs Data = new LocationsCs();
            Data.Id = "0";
            Data.Location = "0";
            Context.Response.Write(Js.Serialize(Data));
        }
        else
        {
            List<LocationsCs> Locations = new List<LocationsCs>();
            for (int i = 0; i < Dt.Rows.Count; i++)
            {
                Locations.Add(new LocationsCs
                {
                    Id = Dt.Rows[i]["Id"].ToString(),
                    Location = Dt.Rows[i]["Location"].ToString(),
                });
            }
            //string  strResponse = Js.Serialize(Data);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
            HttpContext.Current.Response.Write(Js.Serialize(Locations));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
    }

    public class LocationsCs
    {
        public String Id;
        public String Location;
    }

this works for me.

冷情妓 2025-02-09 04:16:59
 this is  works for me also.

 ResponseError err = new ResponseError();
   List<ResponseError> errorDetails = new List<ResponseError>();
   public class ResponseError
    {
        //public List<ErrorDetails> Error { get; set; }
        public string ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
        public string ISACTicketNumber { get; set; }
    }
       errorDetails.Add(err);

            
            var outputJson = err;
JavaScriptSerializer Js = new JavaScriptSerializer();
            var jresp = JsonConvert.SerializeObject(outputJson, Formatting.Indented);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType ="application/json; charset=utf-8";
            HttpContext.Current.Response.Write(Js.Serialize(outputJson));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //HttpContext.Current.Response.End();
           
            return outputJson;
 this is  works for me also.

 ResponseError err = new ResponseError();
   List<ResponseError> errorDetails = new List<ResponseError>();
   public class ResponseError
    {
        //public List<ErrorDetails> Error { get; set; }
        public string ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
        public string ISACTicketNumber { get; set; }
    }
       errorDetails.Add(err);

            
            var outputJson = err;
JavaScriptSerializer Js = new JavaScriptSerializer();
            var jresp = JsonConvert.SerializeObject(outputJson, Formatting.Indented);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType ="application/json; charset=utf-8";
            HttpContext.Current.Response.Write(Js.Serialize(outputJson));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //HttpContext.Current.Response.End();
           
            return outputJson;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文