从具有相同ID的另一部分提取数据(Appcelerator)

时间:2012-08-27 02:04:04

标签: javascript json parsing appcelerator

我有以下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的部分,我需要获取最终成绩,百分比和术语。< / p>

任何人都可以帮我吗?我一直试着用了两天试图解决这个问题......

2 个答案:

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

答案 1 :(得分:0)

部分 - &gt;最终成绩

在部分列表中,我将在tablerow中传递一个变量,用于查询下一组数据。您需要使用section 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。