下面是JSON輸出我得到在客戶端:轉換JSON對象輸出到jQuery的陣列格式
jsonp1363710839478({"Comment":[{"Author":"Plan_A","CommentText":"AI like hay almost as much as I like sun. Just joking","Title":"AMaking hay when the sun shines"},{"Author":"Plan_B","CommentText":"I like hay almost as much as I like sun. Just joking","Title":"Making hay when the sun shines"}]});
如何將其轉換爲下面的例子:
var sample= [
{ Title: "The Red Violin", Author: "1998" },
{ Title: "Eyes Wide Shut", Author: "1999" },
{ Title: "The Inheritance", Author: "1976" }
];
下面是我的全部代碼:
後面的代碼:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public CommentList GetComments()
{
Comments oComment1 = new Comments();
oComment1.Title = "AMaking hay when the sun shines";
oComment1.Author = "Plan_A";
oComment1.CommentText = "AI like hay almost as much as I like sun. Just joking";
Comments oComment2 = new Comments();
oComment2.Title = "Making hay when the sun shines";
oComment2.Author = "Plan_B";
oComment2.CommentText = "I like hay almost as much as I like sun. Just joking";
CommentList oCommentList = new CommentList();
oCommentList.Comment.Add(oComment1);
oCommentList.Comment.Add(oComment2);
return oCommentList;
}
客戶端調用jQuery的:
$('#CommentsButton').click(function() {
$.getJSON('http://localhost:55679/RESTService.svc/GetComments?callback=?', function (data) {
for (var i = 0; i < data.Comment.length; i++) {
alert(data.Comment[i].Author);
}
});
我是新來的Jquery如此詳細的解釋/代碼,將不勝感激。
你想改變客戶端收到什麼? – NickSlash 2013-03-19 18:06:55
@nickslash是請 – Khan 2013-03-19 18:46:36