Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / April 2008

Tip: Looking for answers? Try searching our database.

ASMX Json Serialization in 3.5 Framework.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
batham - 01 Apr 2008 16:29 GMT
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; }
}
Michael Justin - 01 Apr 2008 17:08 GMT
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
batham - 01 Apr 2008 17:25 GMT
On Apr 1, 9:08 am, Michael Justin <michael.jus...@nospam.gmx.net>
wrote:
> batham schrieb:
>
[quoted text clipped - 9 lines]
>
> Michael

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

Thanks,
Shailendra

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.