5

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!

Nico
  • 51
  • 1
  • 1
  • 2

4 Answers4

2

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 () { ... }
    });
Simone S.
  • 1,756
  • 17
  • 18
  • 1
    I had to set `automaticFormatSelectionEnabled` to false in my config otherwise I ended up with an XML resposne rather than a JSON one, otherwise this worked a treat. – Zhaph - Ben Duguid Dec 04 '12 at 15:35
1

I don't think that you can. I think that is the format of the json that is returned.

You can try get rid of the ResponseFormat bit and return a string and use

JavaScriptSerializer ser = new JavaScriptSerializer(); 
string json = ser.Serialize(objectClass);
return json;

Or even better use the JSON.Net libraries.

Also look at How to not serialize the __type property on JSON objects for how to remove the __type bit.

Community
  • 1
  • 1
Dave Walker
  • 3,498
  • 1
  • 24
  • 25
0

I had the same problem, my solution is using Newtone Json Convert:

return JsonConvert.DeserializeObject<List<TestDTO>>(JsonConvert.SerializeObject(result))

you can also using this solution in aspx's web pages (web method)

-2

If you're using ServiceStack.Text JSON Serializer you just need to:

JsConfig.ExcludeTypeInfo = true;

This functionality was automatically added back in v2.28, but the code above keeps that out of the serialization. You can also change this behavior by Type with:

JsConfig<Type>.ExcludeTypeInfo = true;
Brett Veenstra
  • 47,674
  • 18
  • 70
  • 86