0

我有以下 JSON:(它为你简化了一点)

{ returnJSON = {
    studentDataVOs = {
        finalGrades = (
            {
                grade = A;
                percent = 100;
                sectionid = 7744;
                reportingTermId = 801;
            },
            {
                grade = B+;
                percent = 89;
                sectionid = 7745;
                reportingTermID = 801;
            });
        reportingTerms = (
            {
                id = 801;
                title = S1;
            },
            {
                id = 802;
                title = S2;
            });
        sections = (
            {
                id = 7744;
                termID = 801;
                courseTitle = Physics;
                courseCode = 88A;
            },
            {
                id = 7745;
                termID = 801;
                courseTitle = Government;
                courseCode = 90B;
            });
        };
    };
}

我正在使用 Appcelerator Titanium 构建一个应用程序,该应用程序显示一个表格视图,其中的数据有望显示以下内容:


物理 (88A) - S1(等级:A,100%)


政府 (90B) - S1(等级:B+,89%)


...等等...

我设置了表格视图,以下代码从部分中提取数据并将其放在表格视图的标签中:

var response = JSON.parse(response);
var sections = response.returnJSON.studentDataVOs.sections;
for (i=0;i<sections.length;i++) {
    var courseName = sections[i].courseTitle;
    var courseCode = sections[i].courseCode;
}

我无法弄清楚如何获取每个班级的成绩和学期名称。如您所见,每个部分的数据都包含一个 ID 和 termID,它们将我引导到 finalGrades 和 reportingTerms 中包含 ID 或 termID 的部分,我需要在其中获取最终成绩、百分比和学期标题。

谁能帮我这个?我已经尝试了两天试图解决这个问题......

4

2 回答 2

0

部分 -> 最终成绩

在部分列表中,我将在 tablerow 中传递一个变量,我可以使用它来查询下一组数据。您需要部分 id 将数据与最终成绩中的数据相关联,因此我会将其添加到我的表格行中。创建表时:

// This loop to loop through the data.
function getSections(_args){
   var response = JSON.parse(response);
   var sections = response.returnJSON.studentDataVOs.sections;
    for (i=0;i<sections.length;i++) {
       createMyRow({
         courseName: sections[i].courseTitle,
         courseCode: sections[i].courseCode,
         sectionId: sections[i].id
       });
    }
}

// This function creates the rows for the table 
function createMyRow(_args){
   // the sectionId is now included in the tableRow and can be used later for your 
   // next query.
   // allows the sectionId value is now can be queried when a user clicks it 
   // through e.rowData.
   var tableRow = Ti.UI.createTableViewRow({
      sectionId: _args.sectionId
   });

   var title = Ti.UI.createLabel({
      text: _args.courseName
   });
   tableRow.add(title);

    return tableRow;
    }

    var tableView = Ti.UI.createTableView();
var data = [];
function refreshTable()

   data = getSections();
   var rows = [];
   for( var i = 0; i<data.length; i++){
      rows.push(createMyRow(data[i]);
   }
   tableView.setData(rows);
}

// this passes the value of the sectionId so you can use it to look up your next section.
tableView.addEventListener('click', function(e){
   getFinalGrades({
      sectionId: e.rowData.sectionId
   })
});
function getFinalGrades(_args){
   var finalGrades = response.returnJSON.studentDataVOs.finalGrades
   var data = [];
   for (i=0;i<finalGrades.length;i++) {
     if(finalGrades[i].sectionId == _args.sectionId){
       data.push({
         grade: finalGrades[i].grade,
         percent: finalGrades[i].percent
       });
     }
   }
   return data;
}

通过查看我的一些代码示例,我很快就将其破解了。在 getFinalGrades 函数的末尾,您将拥有一个 finalGrades 数组,它们仅来自您单击的 sectionId。

于 2012-08-27T13:47:32.187 回答
0

您应该为列出的每个字段创建索引。这很简单:

//for example you have user list
var users = [{
    id : 1, email : 'user@gmail.com',
    nickname : 'John'
}, {
    id : 2, email : 'user@stackoverflow.com',
    nickname : 'Peter'
}];

//and you need to query user by his email, id and nickname
//first, create 3 index list

var usersIdIndex = {}, usersEmailIndex = {},
    usersNicknameIndex = {};

//then fill them

users.forEach(function(user){
    usersIdIndex[user.id] = user;
    usersEmailIndex[user.email] = user;
    usersNicknameIndex[user.nickname] = user;
});

//now you can get users by any of this fields
//for example

console.log(usersIdIndex[2].nickname== 'Peter');
console.log(usersNicknameIndex['John'].email == 'user@gmail.com');
于 2012-08-27T04:40:14.343 回答