PC Review


Reply
Thread Tools Rate Thread

ASMX Json Serialization in 3.5 Framework.

 
 
batham
Guest
Posts: n/a
 
      1st Apr 2008
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; }
}
 
Reply With Quote
 
 
 
 
Michael Justin
Guest
Posts: n/a
 
      1st Apr 2008
batham schrieb:

> {"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
 
Reply With Quote
 
batham
Guest
Posts: n/a
 
      1st Apr 2008
On Apr 1, 9:08*am, Michael Justin <michael.jus...@nospam.gmx.net>
wrote:
> batham schrieb:
>
> > {"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


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

Thanks,
Shailendra
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASMX in Framework 4.0 Eugene van der Walt Microsoft ASP .NET 0 13th Jul 2010 09:31 AM
How Do I Create Nested JSON Objects for Serialization (Using MyJsonHelper Class)? pbd22 Microsoft C# .NET 5 22nd Jul 2009 12:20 AM
ASMX Json Serialization in 3.5 Framework. batham Microsoft C# .NET 2 1st Apr 2008 05:25 PM
Serialization - bug in framework. =?Utf-8?B?Qmpvcm4=?= Microsoft Dot NET 1 4th Jan 2005 06:45 PM
Serialization bug between framework 1.0 and 1.1 B.J. Microsoft Dot NET Framework 0 11th Jul 2003 09:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:14 AM.