将Json对象输出转换为Jquery数组格式

时间:2013-03-19 18:01:42

标签: c# asp.net .net wcf

以下是我在客户端获得的Json输出:

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的新手,所以详细说明/代码将不胜感激。

2 个答案:

答案 0 :(得分: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};
}

答案 1 :(得分: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;
}