如何解析 WCF DataContract 对象创建的 JSON

发布于 2024-11-08 17:37:17 字数 773 浏览 0 评论 0原文

我有一个用 [DataContract] 属性装饰的对象,我的 WCF 服务将其作为 JSON 返回,如下所示:

{"GetCommentsByPostResult":[{"CommentCreated":"\/Date(1305736030505+0100)\/","CommentText":"Comment 1"},{"CommentCreated":"\/Date(1305736030505+0100)\/","CommentText":"Comment 2"},{"CommentCreated":"\/Date(1305736030505+0100)\/","CommentText":"Comment 2"}]});

我尝试使用此 jQuery 代码迭代 CommentCreated:

$(data).each(function () 
{
     alert(this.CommentCreated);
});

但我得到的只是一个带有“未定义”的警报框,因此我更改了它到:

 $(data).each(function () {

         $(this.GetCommentsByPostResult).each(function () {

                      alert(this.GetCommentsByPostResult);
         });

 });

但这仍然不起作用。我想做的是迭代 CommentCreated 并将它们扔到警报框中......

I have an object decorated with [DataContract] attribute and my WCF service is returning this as JSON like this:

{"GetCommentsByPostResult":[{"CommentCreated":"\/Date(1305736030505+0100)\/","CommentText":"Comment 1"},{"CommentCreated":"\/Date(1305736030505+0100)\/","CommentText":"Comment 2"},{"CommentCreated":"\/Date(1305736030505+0100)\/","CommentText":"Comment 2"}]});

Ive attempted to iterate through the CommentCreated with this jQuery code:

$(data).each(function () 
{
     alert(this.CommentCreated);
});

But all I get is an alert box with 'undefined in' so I changed it to:

 $(data).each(function () {

         $(this.GetCommentsByPostResult).each(function () {

                      alert(this.GetCommentsByPostResult);
         });

 });

but that still doesnt work. What I want to do is iterate the CommentCreated and throw them to an alert box....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2024-11-15 17:37:18

我不确定,但我不认为 this 会是调用 each 时的当前元素。另外,为什么要使用 jQuery 函数包装 data 变量? jQuery 集合适用于 DOM 元素。

$.each(data.GetCommentsByPostResult, function (e) {
    alert(e.CommentCreated);
});

I'm not sure, but I don't think this would be the current element when calling each. Also, why are you wrapping the data variable with the jQuery function? jQuery collections are for DOM elements.

$.each(data.GetCommentsByPostResult, function (e) {
    alert(e.CommentCreated);
});
渔村楼浪 2024-11-15 17:37:18

根据您获取数据的方式(库?自定义代码?),您很可能必须首先将 JSON 字符串转换为实际的 JavaScript 对象。许多浏览器都内置了执行此操作的方法,但您可能需要利用第三方库来利用那些没有现成支持的库。我会推荐 JSON-js 因为它遵循与某些浏览器内置的语法相同的语法。

var obj = JSON.parse(data);

拥有此对象后,您现在可以使用标准 JavaScript 点或括号表示法访问数据:

var comments = obj.GetCommentsByPostResult; // or...
var comments = obj['GetCommentsByPostResult'];

这两行是等效的。要迭代注释,正如您尝试做的那样,您可以尝试:

$.each(obj.GetCommentsByPostResult, function (e) {
    alert(e.CommentCreated);
});

另外,我建议使用 console.log() 而不是alert(),以及支持检查记录对象的浏览器。这将是带有 Firebug 扩展的 FireFox,或者带有其开发人员工具的 Chrome(Ctrl-Shift-J 来激活)。不确定这种类型的工具在 IE9 中的状态,但如果那里也有等效的工具,我不会感到惊讶。

Depending on how you are acquiring the data (a library? custom code?) you will most likely have to convert the JSON string first into an actual JavaScript object. Many browsers have built in methods for doing this, though you may need to take advantage of a 3rd party library to take advantage of those that do not have out-of-box support. I would recommend JSON-js as it follows the same syntax as those found built into some browsers.

var obj = JSON.parse(data);

Once you have this object, you can now access the data with standard JavaScript dot or bracket notation:

var comments = obj.GetCommentsByPostResult; // or...
var comments = obj['GetCommentsByPostResult'];

Those two lines are equivalent. To iterate the comments, as you are trying to do, you could try:

$.each(obj.GetCommentsByPostResult, function (e) {
    alert(e.CommentCreated);
});

Also, I would recommend using console.log() instead of alert(), and a browser that supports inspection of the logged objects. This would be FireFox with the Firebug extension, or Chrome with their developer tools (Ctrl-Shift-J to activate). Not sure about the status of this type of tool in IE9, though I would not be surprised if there was an equivalent tool there as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文