如何解析 WCF DataContract 对象创建的 JSON
我有一个用 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定,但我不认为
this
会是调用each
时的当前元素。另外,为什么要使用 jQuery 函数包装data
变量? jQuery 集合适用于 DOM 元素。I'm not sure, but I don't think
this
would be the current element when callingeach
. Also, why are you wrapping thedata
variable with the jQuery function? jQuery collections are for DOM elements.根据您获取数据的方式(库?自定义代码?),您很可能必须首先将 JSON 字符串转换为实际的 JavaScript 对象。许多浏览器都内置了执行此操作的方法,但您可能需要利用第三方库来利用那些没有现成支持的库。我会推荐 JSON-js 因为它遵循与某些浏览器内置的语法相同的语法。
拥有此对象后,您现在可以使用标准 JavaScript 点或括号表示法访问数据:
这两行是等效的。要迭代注释,正如您尝试做的那样,您可以尝试:
另外,我建议使用 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.
Once you have this object, you can now access the data with standard JavaScript dot or bracket notation:
Those two lines are equivalent. To iterate the comments, as you are trying to do, you could try:
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.