从ASP Web服务的JSON响应和__type:如何除去D- [英] How to remove d: and __type from JSON response for ASP web service

查看:245
本文介绍了从ASP Web服务的JSON响应和__type:如何除去D-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个在网络上是为WCF的Web服务,而不是ASP Web服务。

几种解决方案

目前,我取回一个JSON响应,上面写着:

<$p$p><$c$c>{\"d\":[{\"__type\":\"NetworkFuzzWebSvc.Sessions\",\"BaseUri\":\"http://localbox\",\"SessionId\":\"43b8716f-40ab-43bf-8311-575c2ecd2730}]}

我需要它返回:

<$p$p><$c$c>{\"Sessions\":[\"BaseUri\":\"http://localbox\",\"SessionId\":\"43b8716f-40ab-43bf-8311-575c2ecd2730}]}

下面是web服务code的副本,我使用(NetFuzzWebSvc.asmx):

 使用系统;
使用System.Collections.Generic;
使用的System.Web;
使用System.Web.Services;
使用System.Web.Script.Services;命名空间NetworkFuzzWebSvc
{
    公共类会议
    {
        公共字符串的基本URI;
        公共字符串的SessionID;
    }    ///&LT;总结&gt;
    ///为NetFuzzJson摘要说明
    ///&LT; /总结&gt;
    [WebService的空间(namespace =HTTP:// localbox的)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)    [ScriptService]
    公共类NetFuzzJson:WebService的
    {
        清单&LT;会议&GT;会话=新的List&LT;会议&GT;
        {
            新的会话{
                        基本URI =HTTP:// localbox的/,
                        SESSIONID =43b8716f-40AB-43bf-8311-575c2ecd2730
            }
        };        [的WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)
        公开名单&LT;会议&GT; GetAllSessions()
        {
            返回会议;
        }
    }

任何人有一个解决的办法?
谢谢!


解决方案

有关删除D和__type

.SVC

  [的ServiceContract]
公共接口ITestService
{
    [OperationContract的]
    [WebInvoke(方法=POST,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.WrappedRequest)
    清单&LT; TestDTO&GT;得到所有();
}

的.config

 &LT;&行为GT;
  &LT; endpointBehaviors&GT;
    &LT;行为NAME =DebugJSonBehavior&GT;
      &LT; enableWebScript /&GT;
      &LT;! - 需要设置automaticFormatSelectionEnabled属性 - &GT;
      &LT; webHttp automaticFormatSelectionEnabled =真/&GT;
    &LT; /行为&GT;
  &LT; / endpointBehaviors&GT;
  &LT; serviceBehaviors&GT;
    &LT;行为NAME =DebugJSonBehavior&GT;
      &LT; serviceMetadata httpGetEnabled =真/&GT;
      &LT; serviceDebug httpHelpPageEnabled =真includeExceptionDetailInFaults =真/&GT;
    &LT; /行为&GT;
  &LT; / serviceBehaviors&GT;
&LT; /行为&GT;

记者:

  $阿贾克斯({
        键入:POST,
        网址:_serviceUrl +/TestService.svc/GetAll
        数据:{},
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON
        成功:函数(dataret){...},
        错误:功能(xmlHtt prequest,textStatus,errorThrown){...},
        完成:功能(){...}
    });

I've found several solutions for this on the web that are for WCF web service and not ASP web service.

Currently, I'm getting back a JSON response that says:

{"d":[{"__type":"NetworkFuzzWebSvc.Sessions","BaseUri":"http://localbox","SessionId":"43b8716f-40ab-43bf-8311-575c2ecd2730}]}

I need it to return:

{"Sessions":["BaseUri":"http://localbox","SessionId":"43b8716f-40ab-43bf-8311-575c2ecd2730}]}

Here is a copy of the webservice code that I'm using (NetFuzzWebSvc.asmx):

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace NetworkFuzzWebSvc
{
    public class Sessions
    {
        public string BaseUri;
        public string SessionId;
    }

    /// <summary>
    /// Summary description for NetFuzzJson
    /// </summary>
    [WebService(Namespace = "http://localbox")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ScriptService]
    public class NetFuzzJson : WebService
    {
        List<Sessions> Sessions = new List<Sessions>
        {
            new Sessions{
                        BaseUri = "http://localbox/", 
                        SessionId="43b8716f-40ab-43bf-8311-575c2ecd2730"
            }
        };

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public List<Sessions> GetAllSessions()
        {
            return Sessions;
        }
    }  

Anyone have a solution to this? Thanks!

解决方案

For remove "d" and "__type":

.svc

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    [WebInvoke(Method = "POST",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    List<TestDTO> GetAll();
}

.config

<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>

Js:

    $.ajax({
        type: "POST",
        url: _serviceUrl + "/TestService.svc/GetAll",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (dataret) { ... },
        error: function (xmlHttpRequest, textStatus, errorThrown) {... },
        complete: function () { ... }
    });

这篇关于从ASP Web服务的JSON响应和__type:如何除去D-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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