What does .d in JSON mean?
Asked Answered
F

9

51

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?

Flaunch answered 6/5, 2009 at 15:15 Comment(2)
related: #740359Sabinesabino
related: #2812025 NB I've changed my opinion on my VTC on this question - this question despite being newer is more focused and has better answersSabinesabino
A
25

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

Antons answered 6/5, 2009 at 15:20 Comment(5)
thats true, I guess it could be a , b or c but they choose d ( no reason behind this I guess) just a security feature that Microsoft added in ASP.NET 3.5 + version of ASP.NET AJAX by encapsulating the JSON response within a parent object.Psychophysiology
I think 'd' for dataOkinawa
Is there a reason to get rid of this or change it? It's unsettling to have so much JS depending on this random "d" parameter that you don't control. What if some version changes it to "f"?Lakia
@mmcrae One year later and I have the same concern, any updates? Did you ever find out?Lailaibach
@Lailaibach - the code I wrote that has result.d all over is still running and working in Production shrug. I guess it's just part of doing a AJAX call with a Web Forms web result end point. The "related" links in comments under the OP question I think are good for discussion of what the .d is from. --- as usual, if only there was better DOCUMENTATION...Lakia
E
12

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

Equilateral answered 5/6, 2014 at 16:19 Comment(2)
Is it possible to change or get rid of the "d"?Lakia
@mmcrae Yes, it's possible with direct writing to Response. Here is the guide: https://mcmap.net/q/355195/-can-39-t-remove-d-encapsulation-from-json-response-in-netGianina
M
2

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

    }
Moniliform answered 5/6, 2015 at 8:19 Comment(2)
It is always helpful to add some explanation to your codeSelfemployed
Why should I read this? Is it an answer, an explanation, an example of the problem?Lakia
F
1

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

Flaminius answered 29/5, 2014 at 5:32 Comment(0)
A
0

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.

Abaft answered 6/5, 2009 at 15:16 Comment(0)
C
0

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.

Cloakroom answered 6/5, 2009 at 15:17 Comment(2)
My method just returns a string, e.g. return "blablabla". Where is the d set?Flaunch
Is it possible to change or get rid of the "d"?Lakia
A
0

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];
Associative answered 6/5, 2009 at 15:20 Comment(0)
B
0

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>
Butyrin answered 5/6, 2015 at 8:46 Comment(0)
K
-1

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. :)

Kent answered 2/3, 2020 at 17:55 Comment(1)
Take a look again at How to Answer to see that answers should be more complete than thisBronchi

© 2022 - 2024 — McMap. All rights reserved.