What does .d in JSON mean?
Are you referring to the ADO.NET Data Services?
I remember hearing a presentation about the JSON returning this and I think its just a wrapper to ensure the payload is a JSON object as opposed to an array (which is the case of returning multiple entities).
Why 'd' specifically? I think I remember them saying something like 'well it had to be something'.
Based on this tutorial: JSON Web Service And jQuery with Visual Studio 2008
The Web Method returns a Product that is serialized in JSON format. Since there is not JSON
type, the returned value is a String
with JSON format.
On the client side, the ajax call returns a JSON.
The result looks like {d: 'returned-string-with-JSON-format'}
More exactly something like: {d:'{"ID":123,"Name":"Surface Pro 2"}'}
Note that 'returned-string-with-JSON-format'
is a string not a JSON object so you cannot do result.d.ID
.
Instead you need to convert it to JSON object by using JSON.parse(result.d)
or eval(result.d)
At the end, what you really want is do this:
result = JSON.parse(result.d)
UPDATEAlso consider this demo, where I use a JSON in string format and convert it to JSON object:
ASPX Code Here:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <script type="text/javascript"> function GetData() { alert("I am called"); $.ajax({ type: "POST", url: "Contact.aspx/GetProducts", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { var data = JSON.parse(result.d) alert(data.Id); }, error:function(ex) { alert("Test"); } }); } </script> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="GetData();" /></asp:Content>
C# Code Here:
public partial class Contact : Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindList(); } int[] arr1 = new int[] { 1, 2 }; ListBox1.SelectedValue = "1"; ListBox1.SelectedValue = "4"; } void BindList() { List<Product> lst = new List<Product>() { new Product{Id=1,Name="Photo"}, new Product{Id=2,Name="Photo"}, new Product{Id=3,Name="Photo"}, new Product{Id=4,Name="Photo"} }; ListBox1.DataSource = lst; ListBox1.DataTextField = "Name"; ListBox1.DataValueField = "Id"; ListBox1.DataBind(); } [WebMethod] public static string GetProducts() { // instantiate a serializer JavaScriptSerializer TheSerializer = new JavaScriptSerializer(); //optional: you can create your own custom converter // TheSerializer.RegisterConverters(new JavaScriptConverter[] { new MyCustomJson() }); //var products = context.GetProducts().ToList(); Product products = new Product() { Id = 1, Name = "Testing Services" }; var TheJson = TheSerializer.Serialize(products); return TheJson; } }