What does .d in JSON mean?

I have a .NET webmethod that I have called from jQuery. The method returns some HTML markup that I display within a DIV element.

Once I have the response I use

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

My question is, what does the .d do? I don’t like using code I don’t fully understand? Could I get the same result using Eval?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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’.

Method 2

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)

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

enter image description here

Method 3

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

    }

Method 4

may be very much useful link for those who want to really learn from scratch an example about wrapper class where the details in one class can never be dislosed to other class but can be indirectly accessed through various methods
http://www.c-sharpcorner.com/Blogs/12038/wrapper-class-in-C-Sharp.aspx

Method 5

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

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

Method 6

The d is part of the result returned by your .NET code. If you look at this code you should see a variable being set with the name d. If it is generated from serialized classes, then it probably sends along a member of that class with the name d.

Method 7

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

var property = "d";
var value = result[property];

Method 8

Its is very clear that $(“#div”).html(result.d); in your code

“result” is a object and d is property of “result”.

Let explain,

if you create object like this,

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

if we access the property of result is that using jquery

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

so we get result in html is

<html>day</html>

Method 9

Once, my friend told me that “d” represent “data” from response that ajax get in return(because response can contain a lot more than just simple data).

Maybe it is, maybe it isn’t but still you can accept it like it is. 🙂


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x