如何根据页面大小打破json字符串?

时间:2014-10-30 15:09:54

标签: javascript jquery arrays json

我在jquery中有点新秀,并试图提高技能。 我从ajax调用得到了json字符串。现在我只需要根据网格页面大小来划分它;

我的逻辑就是这个。

首先,我已经在字符串

中取出了长度的对象
var _totalLength = returnedData.length;
var _pageSize = 10;
var _totalPageRequired = Math.ceil(returnedData.length / _pageSize);
var array = new Array(_totalPageRequired);

现在我只需创建一个数组并根据页面大小放置记录。

var array = new Array(_totalPageRequired);

现在我运行for循环直到_totalPageRequired

for (var i = 0; i < _totalPageRequired; i++) {
//here i am stucked, i want to know how could i assign
//per array[i] = the required data from the json string
// _totalLength = 43 and _pageSize = 10 therefore totalpagedata required is 5
// for ex:  array[0] = returnedData[from index 0 to 9]
// array[1] = returnedData[from index 10 to 19] and so on
// array[4] = returnedData[from index 40 - 43] as records end 
}

任何人帮我打破这个json字符串。

编辑:我的Json字符串就像

[{"UserRates":null,"RoleId":2,"Name":"M.Hassaan Khan","RoleName":null,"Password":null,"EmployeeRate":null,"Gender":null,"CategoryId":0,"CategoryName":null,"ClientId":0,"ClientName":null,"UserId":2,"Username":"hassaan09","IsMapped":false,"ProjectId":0,"ProjectName":null,"ProjectTitle":null,"CreationDate":"0001-01-01T00:00:00","ExpectedEndingdate":"0001-01-01T00:00:00","Description":null,"ParentProjectId":0,"Cost":0.0,"Status":false},{"UserRates":null,"RoleId":2,"Name":"Bilawaal","RoleName":null,"Password":null,"EmployeeRate":null,"Gender":null,"CategoryId":0,"CategoryName":null,"ClientId":0,"ClientName":null,"UserId":5,"Username":"test_09","IsMapped":false,"ProjectId":0,"ProjectName":null,"ProjectTitle":null,"CreationDate":"0001-01-01T00:00:00","ExpectedEndingdate":"0001-01-01T00:00:00","Description":null,"ParentProjectId":0,"Cost":0.0,"Status":false},{"UserRates":null,"RoleId":2,"Name":"Zardari","RoleName":null,"Password":null,"EmployeeRate":null,"Gender":null,"CategoryId":0,"CategoryName":null,"ClientId":0,"ClientName":null,"UserId":18,"Username":"register_09","IsMapped":false,"ProjectId":0,"ProjectName":null,"ProjectTitle":null,"CreationDate":"0001-01-01T00:00:00","ExpectedEndingdate":"0001-01-01T00:00:00","Description":null,"ParentProjectId":0,"Cost":0.0,"Status":false},{"UserRates":null,"RoleId":2,"Name":"Nawaz","RoleName":null,"Password":null,"EmployeeRate":null,"Gender":null,"CategoryId":0,"CategoryName":null,"ClientId":0,"ClientName":null,"UserId":30,"Username":"testing111","IsMapped":false,"ProjectId":0,"ProjectName":null,"ProjectTitle":null,"CreationDate":"0001-01-01T00:00:00","ExpectedEndingdate":"0001-01-01T00:00:00","Description":null,"ParentProjectId":0,"Cost":0.0,"Status":false}]

在ajax成功之后我正在做Json.Parse(data.d)所以它将它转换为对象;

2 个答案:

答案 0 :(得分:1)

试试这个:http://jsfiddle.net/aras7/b83uzccn/1/

var _totalLength = returnedData.length;
var _pageSize = 2;

var array = [];

var page = 0;
for(var i = 0;i < returnedData.length;){
    var page_data = []; 
    for(var j = 0;j < _pageSize && i < returnedData.length;j++) {
        page_data.push(returnedData[i]);
        i++;
    }
    array.push(page_data)
}

答案 1 :(得分:0)

在我的手机上打字,所以它没有解释,希望它有所帮助,是你正在寻找的。

var _totalLength = returnedData.length;
var _pageSize = 10;
var array = [];
var i = -1;
var currenPage:
while(++i< _totalLength){
  currentPage = Math.floor(i/_pageSize);
  array [currentPage] =   array [currentPage] || [];
  array [currentPage].push(returnedData[i]);
}