What does .d mean in JSON?

I have a .NET web method that I called from jQuery. The method returns some HTML markup that I show inside the DIV element.

As soon as I have an answer, I use

$("#div").html(result.d); 

My question is: what does .d do? I don’t like using code that I don’t quite understand? Can I get the same result using Eval?

+41
json jquery ajax
May 6, '09 at 15:15
source share
8 answers

Do you mean ADO.NET data services?

I remember hearing a presentation that JSON returns this, and I think its just a wrapper for ensuring that the payload is a JSON object , as opposed to an array (in the case of returning multiple objects).

Why exactly? I think I remember how they said something like "good that it should be something."

+21
May 6, '09 at 15:20
source share

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 the JSON type does not exist, the return value is String with the JSON format.

On the client side, ajax call returns JSON.

The result looks like {d: 'returned-string-with-JSON-format'}

More precisely, something like: {d:'{"ID":123,"Name":"Surface Pro 2"}'}

Please note that 'returned-string-with-JSON-format' is a string that is not a JSON object, therefore it cannot do result.d.ID .

Instead, you need to convert it to a JSON object using JSON.parse(result.d) or eval(result.d)

In the end, you really want to do this:

 result = JSON.parse(result.d) 

UPDATE Also consider this demo, where I use JSON in string format and convert it to a JSON object:

enter image description here

+7
Jun 05 '14 at 16:19
source share

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; } } 
+2
Jun 05 '15 at 8:19
source share

can be a very useful reference for those who want to really learn from scratch, for example, about a wrapper class, where details in one class can never be revealed to another class, but can be indirectly accessed using various http: // www methods . c-sharpcorner.com/Blogs/12038/wrapper-class-in-C-Sharp.aspx

+1
May 29 '14 at 5:32
source share

It returns the value of a field named ' d ' in the object ' result '.

This question shows an example of how JSON might look, pay attention to the d: field.

0
May 6 '09 at 3:16 p.m.
source share

d is part of the result returned by your .NET code. If you look at this code, you will see a variable called d. If it is generated from serialized classes, then it probably sends a member of this class named d.

0
May 6 '09 at 03:17
source share

As others have pointed out, it returns the member "d" object "result" .
If you want to have "d" in a variable, you can use this:

 var property = "d"; var value = result[property]; 
0
May 6 '09 at 3:20 p.m.
source share

It is very clear that $ ("# div"). html (result.d); in your code

"result" is the object, and d is the property of the "result".

Explain

if you create such an object,

 var result{"id": "number", "d": "day"}; 

if we access the property of the result, this is using jquery

 $("#div").html(result.d); 

so we get the result in html

 <html>day</html> 
0
Jun 05 '15 at 8:46
source share



All Articles