jQuery JSON 中的 .d 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/830112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does .d in JSON mean?
提问by Fermin
I have a .NET webmethod that I have called from jQuery. The method returns some HTML markup that I display within a DIV element.
我有一个从 jQuery 调用的 .NET webmethod。该方法返回一些我在 DIV 元素中显示的 HTML 标记。
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?
我的问题是,.d 有什么作用?我不喜欢使用我不完全理解的代码?我可以使用 Eval 得到相同的结果吗?
采纳答案by Clinton
Are you referring to the ADO.NET Data Services?
您指的是 ADO.NET 数据服务吗?
I remember hearing a presentation about the JSON returning this and I think its just a wrapper to ensure the payload is a JSON objectas opposed to an array (which is the case of returning multiple entities).
我记得听过一个关于 JSON 返回这个的演示,我认为它只是一个包装器,以确保有效负载是一个 JSON 对象,而不是一个数组(这是返回多个实体的情况)。
Why 'd' specifically? I think I remember them saying something like 'well it had to be something'.
为什么是“d”?我想我记得他们说过类似“好吧,它必须是某种东西”之类的话。
回答by Jaider
Based on this tutorial: JSON Web Service And jQuery with Visual Studio 2008
基于本教程:JSON Web Service 和 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.
Web 方法返回一个以 JSON 格式序列化的产品。由于没有JSON
类型,返回值为a String
with JSON format。
On the client side, the ajax call returns a JSON.
在客户端,ajax 调用返回一个 JSON。
The result looks like {d: 'returned-string-with-JSON-format'}
结果看起来像 {d: 'returned-string-with-JSON-format'}
More exactly something like: {d:'{"ID":123,"Name":"Surface Pro 2"}'}
更确切地说是: {d:'{"ID":123,"Name":"Surface Pro 2"}'}
Note that 'returned-string-with-JSON-format'
is a string not a JSON object so you cannotdo result.d.ID
.
请注意,这'returned-string-with-JSON-format'
是一个字符串而不是 JSON 对象,因此您不能执行result.d.ID
.
Instead you need to convert it to JSON object by using JSON.parse(result.d)
or eval(result.d)
相反,您需要使用JSON.parse(result.d)
或将其转换为 JSON 对象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:
更新还要考虑这个演示,我使用字符串格式的 JSON 并将其转换为 JSON 对象:
回答by Ramdas Chavan
ASPX Code Here:
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# Code Here:
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;
}
}
回答by clarifier
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
对于那些想要真正从头开始学习包装类示例的人来说可能是非常有用的链接,其中一个类中的细节永远不会泄露给其他类,但可以通过各种方法间接访问 http://www.c-sharpcorner .com/Blogs/12038/wrapper-class-in-C-Sharp.aspx
回答by user4977136
Its is very clear that $("#div").html(result.d); in your code
很明显 $("#div").html(result.d); 在你的代码中
"result" is a object and d is property of "result".
“result”是一个对象,d是“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
如果我们访问 result 的属性是使用 jquery
$("#div").html(result.d);
so we get result in html is
所以我们在 html 中得到的结果是
<html>day</html>
回答by Greg
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:
正如其他人指出的那样,它返回对象的"d"
成员"result"
。
如果你想拥有"d"
一个变量,你可以使用这个:
var property = "d";
var value = result[property];
回答by unwind
It returns the value of the field named 'd
' in the object 'result
'.
它返回d
对象“ result
”中名为“ ”的字段的值。
This questionshows an example of how the JSON might look, notice the d:
field.
这个问题显示了 JSON 可能看起来如何的示例,注意该d:
字段。
回答by Marius
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.
d 是 .NET 代码返回的结果的一部分。如果您查看此代码,您应该会看到一个名为 d 的变量。如果它是从序列化类生成的,那么它可能会发送该类的一个名为 d 的成员。
回答by Vlada
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).
有一次,我的朋友告诉我,“d”代表来自 ajax 返回的响应的“数据”(因为响应可以包含的不仅仅是简单的数据)。
Maybe it is, maybe it isn't but still you can accept it like it is. :)
也许是,也许不是,但你仍然可以接受它。:)