2009-05-06 132 views
42

我有一個.NET webmethod,我從jQuery調用。該方法返回一些在DIV元素中顯示的HTML標記。JSON中的.d是什麼意思?

一次,我已經響應我用

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

我的問題是,什麼是.D嗎?我不喜歡使用我不完全理解的代碼?我可以使用Eval獲得相同的結果嗎?

+0

相關:http:// stackov erflow.com/questions/739859/returning-html-from-json-webservice-what-is-the-d – 2012-09-21 01:39:30

+0

相關:http://stackoverflow.com/questions/2811525/removing-the-d-object-from- asp-net-web-service-json-output注意:我在這個問題上改變了我對VTC的看法 - 儘管更新,但這個問題更專注並且有更好的答案 – 2012-09-21 01:43:53

回答

22

您是指ADO.NET數據服務?

我記得聽到關於JSON返回這個的演示文稿,我認爲它只是一個包裝到確保有效負載是一個JSON對象而不是一個數組(這是返回多個實體的情況)。

爲什麼'd'具體?我想我記得他們說'好吧,它必須是某種東西'。

+0

這是真的,我想它可能是A,B或C,但他們選擇D(沒有理由背後,我猜)只是一個安全功能,微軟通過封裝JSON響應在ASP.NET 3.5 +版本的ASP.NET AJAX中添加在父對象內。 – Microtechie 2015-12-21 15:49:29

0

它返回對象'result'中名爲'd'的字段的值。

This question顯示了JSON的外觀示例,請注意d:字段。

0

d是.NET代碼返回結果的一部分。如果你看這個代碼,你應該看到一個名字爲d的變量。如果它是從序列化類生成的,那麼它可能沿着該類的成員發送名稱d。

+0

我的方法只是返回一個字符串,例如返回「blablabla」。 d集在哪裏? – Fermin 2009-05-06 15:20:16

+1

是否有可能改變或擺脫「d」? – mmcrae 2017-01-04 23:28:05

0

正如其他人已經指出的那樣,它返回成員的"result"對象。
如果你想有"d"在一個變量,你可以這樣做:

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

間接訪問:JSON Web Service And jQuery with Visual Studio 2008

Web方法返回一個產品,是連載d以JSON格式。由於不存在JSON類型,因此返回值爲帶有JSON格式的String

在客戶端,ajax調用返回一個JSON。

結果看起來像{d: 'returned-string-with-JSON-format'}

更確切地說是這樣的:{d:'{"ID":123,"Name":"Surface Pro 2"}'}

注意'returned-string-with-JSON-format'是一個字符串不是一個JSON對象,所以你不能result.d.ID

相反,你需要使用JSON.parse(result.d)eval(result.d)

在年底將其轉換成JSON對象,你真正想要的是做到這一點:

result = JSON.parse(result.d) 

UPDATE 還要考慮這個演示,我在字符串格式中使用JSON並將其轉換爲JSON對象:

enter image description here

2

ASPX代碼這裏:

<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#代碼這裏:

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

    } 
0

它很清楚,$( 「#DIV」)HTML(result.d);在您的代碼中

「result」是一個對象,d是「result」的屬性。

讓我們解釋一下,如果你創建了這個對象

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

如果我們訪問結果的屬性是使用jQuery

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

所以我們得到的結果以HTML爲

<html>day</html>