Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - ASMX Json Serialization in 3.5 Framework.

Reply
 
Old 04-01-2008   #1 (permalink)
batham


 
 

ASMX Json Serialization in 3.5 Framework.

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

My System SpecsSystem Spec
Old 04-01-2008   #2 (permalink)
Michael Justin


 
 

Re: ASMX Json Serialization in 3.5 Framework.

batham schrieb:
Quote:

> {"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
My System SpecsSystem Spec
Old 04-01-2008   #3 (permalink)
batham


 
 

Re: ASMX Json Serialization in 3.5 Framework.

On Apr 1, 9:08*am, Michael Justin <michael.jus...@xxxxxx>
wrote:
Quote:

> batham schrieb:
>
Quote:

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

> > 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
So how do I get rid of those? I just want the JSON to be plain without
any extra root nodes.

Thanks,
Shailendra
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
can't update .NET Framework 3.5 Service Pack 1 and .NET Framework 3.5(KB951847 Windows Updates
XML Serialization .NET General
ANN: Bricksoft IM(MSN) SDK For .NET Framework/.NET Compact Framework v1.2 released Live Messenger


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46