【问题标题】:parsing JSON to javascript array将 JSON 解析为 javascript 数组
【发布时间】:2011-07-02 13:51:12
【问题描述】:

我从我的服务调用中得到这个 JSON 响应:

[Object { __type="TestDTO:#TestServer.Data", DateCreated="/Date(1298357607157+0500)/", more...}, Object { __type="TestDTO:#TestServer.Data", DateCreated="/Date(1298357628953+0500)/", more...}]

我想使用需要 javascript 数组的 Jquery 模板

喜欢:

var books = [
            { title: "ASP.NET 4 Unleashed", price: 37.79, picture: "AspNet4.jpg" },
            { title: "ASP.NET MVC Unleashed", price: 44.99, picture: "AspNetMshed.jpg" },
            { title: "ASP.NET Kick Start", price: 4.00, picture: "AspNetKickStart.jpg" },
            { title: "ASP.NET MVC Unleashed iPhone", price: 44.99, picture: "Asele.jpg" },
            ];

如何将 JSSON 响应转换为以下格式(请忽略数据)只是格式应该相同。

我在ajax成功方法中使用这个:

 success: function (response) {
                    var tr = response.d;
                    var tests = tr.Tests;
                      $("#TestsTemplate").tmpl(tests).appendTo("#divTests");

.tmpl(tests) 需要 javascript 数组

Mu Jquery 模板如下所示:

测试名称:${TestName}

测试页:${TestPage} 编号:${ID} 创建日期:${DateCreated} 结果:${结果} 状态:${状态}
                <div id="divTests">

                </div>                 

我从 json 得到的是下面的 DTO

公共类TestRunDTO { 公共长 ID { 获取;放; } 公共字符串 TestSuiteName { 获取;放; } 公共日期时间 DateCreated { 获取;放; } 公共 int 结果 { 获取;放; } 公共 int 状态 { 获取;放; } 公共列表测试{获取;放; } }

我想使用 jquery 模板在 TestRunDTO 中显示这个测试集合

问候, 阿西夫·哈米德

【问题讨论】:

标签: json jquery


【解决方案1】:

您可以将返回的 JSON 转换为 $.tmpl 期望的其他对象。使用这样的东西:

/*
 * returnedObject - data returned by your service call
 */

var returnArr = [];
$.each(returnedObject, function() {
    var _tmp = {};
    _tmp.title = this.title;
    _tmp.price = this.price;
    _tmp.picture = this.picture;
    returnArr.push(_tmp);
});

$.('#template').tmpl(returnArr);

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多