2013-03-19 67 views
0

下面是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如此详细的解释/代码,将不胜感激。

+0

你想改变客户端收到什么? – NickSlash 2013-03-19 18:06:55

+0

@nickslash是请 – Khan 2013-03-19 18:46:36

回答

1
public Comments[] GetComments() 
{ 
    ..... 
    return new Comments[]{oComment1, oComment2}; 
} 

编辑

[OperationContract] 
[WebGet(ResponseFormat = WebMessageFormat.Json)] 
public Comments[] 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"; 

    return new Comments[]{oComment1, oComment2}; 
} 
+0

请问您能解释一下上面的代码吗? – Khan 2013-03-19 22:17:34

+0

@Khan我刚刚改变了返回类型,并在'oComment2.CommentText = .....'之后添加了一行。下面的行可以被删除 – I4V 2013-03-19 22:23:53

0

任何理由在你的JSON响应中的数据不承担任何关于你的第二个代码示例的?

你不只是需要省略你不想(CommentText)的字段和更改注释文本的价值?

[OperationContract] 
[WebGet(ResponseFormat = WebMessageFormat.Json)] 
public CommentList GetComments() 
{ 
    Comments oComment1 = new Comments(); 
    oComment1.Title = "Your Title"; 
    oComment1.Author = "Your Date"; 

    Comments oComment2 = new Comments(); 
    oComment2.Title = "Your Title"; 
    oComment2.Author = "Your Date"; 

    Comments oComment2 = new Comments(); 
    oComment3.Title = "Your Title"; 
    oComment3.Author = "Your Date"; 

    CommentList oCommentList = new CommentList(); 
    oCommentList.Comment.Add(oComment1); 
    oCommentList.Comment.Add(oComment2); 
    oCommentList.Comment.Add(oComment3); 

    return oCommentList; 
}