ASMX Json Serialization in 3.5 Framework.

B

batham

I created a asp.net web site with a web service in it.

My web service returns a object "Person" which is serialized into
JSON, when I make a ajax call to this service this is my return
output.

{"d":{"__type":"Person","FirstName":"Foo","LastName":"Goo","Age":
99,"Address":{"Street":"One Microsoft
Way","City":"Redmond","State":"WA","Zip":98052}}}

My Question is where did that "d' and "__type" come from and is there
a way I can get rid of those two.

I want my end result to be this.

{"FirstName":"Foo","LastName":"Goo","Age":99,"Address":{"Street":"One
Microsoft Way","City":"Redmond","State":"WA","Zip":98052}}

help is really appreciated. I am attaching a copy of the source code.
Nothing has been changed in the web.config.

Thanks,
Shailendra

------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyWebService : System.Web.Services.WebService
{

public MyWebService()
{
}

[WebMethod]
[ScriptMethod(UseHttpGet = true,
ResponseFormat=ResponseFormat.Json)]
public Person CreatePerson()
{
Person p = new Person()
{
FirstName = "Foo",
LastName = "Goo",
Age = 99,
Address = new Address
{
Street = "One Microsoft Way",
City = "Redmond",
State = "WA",
Zip = 98052
}
};
return p;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>:: Test ASMX JSON Serialization ::</title>

<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/
build/yahoo-dom-event/yahoo-dom-event.js"></script>

<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/
build/connection/connection-min.js"></script>

</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
ScriptMode="Release">
<Services>
<asp:ServiceReference Path="~/MyWebService.asmx" />
</Services>
</asp:ScriptManager>
<div>
<a href="javascript:void(0)" id="a1">Call Web Service using
Script Manager</a><br />
<br />
<a href="javascript:void(0)" id="a2">Call Web Service using
YUI</a>
</div>

<script type="text/javascript">
var url = '/WebSite1/MyWebService.asmx/CreatePerson';

function _CallScriptManager(e) {
MyWebService.CreatePerson();
}

function _CallYUI(e) {
YAHOO.util.Connect.initHeader('Content-Type','application/
json',true);
YAHOO.util.Connect.asyncRequest('GET', url, {scope: this,
cache: false});
}

YAHOO.util.Event.addListener('a1', 'click',
_CallScriptManager);
YAHOO.util.Event.addListener('a2', 'click', _CallYUI);
</script>

</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Person
/// </summary>
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public Address Address { get; set; }
}
------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Address
/// </summary>
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
}
 
M

Michael Justin

batham said:
{"d":{"__type":"Person","FirstName":"Foo","LastName":"Goo","Age":
99,"Address":{"Street":"One Microsoft
Way","City":"Redmond","State":"WA","Zip":98052}}}

My Question is where did that "d' and "__type" come from

"d" and "Person" might be name spaces / class names / other metadata of
the serialized object. IIRC this was the case when a Java object is
transformed to JSON using the Apache ActiveMQ message broker.

Michael
 
B

batham

"d" and "Person" might be name spaces / class names / other metadata of
the serialized object. IIRC this was the case when a Java object is
transformed to JSON using the Apache ActiveMQ message broker.

Michael

So how do I get rid of those? I just want the JSON to be plain without
any extra root nodes.

Thanks,
Shailendra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top