function CovertData(jsonData,idField, fromField, toField){
var result = [], curRecord =null, num;
var fromFields = fromField.split(','); // 配列全体をループします: [{...},{...},{...},...]
for(var idx=0;idx
num = findIdx (result, idField, jsonData[idx][idField]);
if(num!=-1){
curRecord = result[num]}
else{
curRecord = {};
};
// 各 json オブジェクトのフィールドをループします
for(var key in jsonData[idx]){
// 変換されたデータ コンテンツを処理します
for( var i=0;iif(key == fromFields[i]){
curRecord[jsonData[idx][toField] '-' fromFields[ i]] = jsonData [idx][key];
break;
}
}
// データ内容に加えて、識別フィールド データのみが処理されます
if(key == idField){
curRecord [key] = jsonData[idx][key]
}
}
if(num==-1){
result.push(curRecord) ;
}
}
結果を返す
}
function findIdx(jsonData, columnName, value){ for(var idx = 0;idxif(jsonData[idx][columnName]==value)
return idx;
}
return -1;
}
JsTestDriver のテストコードは次のとおりです:
TestCase("Test json data row to column",{
setUp :function( ){
this.jsonData = [{年月:201201,ppq:23,spq:27,会社:'dfsoft'},
{年月:201202,ppq:33,spq:38,会社:'dfsoft '},
{年月:201203,ppq:43,spq:49,会社:'dfsoft'},
{年月:201204,ppq:53,spq:51,会社:'dfsoft' },
{年月:201201,ppq:29,spq:26,会社:'vcom'},
{年月:201202,ppq:34,spq:38,会社:'vcom'},
{年月:201203,ppq:48,spq:43,会社:'vcom'},
{年月:201204,ppq:52,spq:59,会社:'vcom'}];
var fromField = 'ppq,spq', toField = '会社', idField = '年月';
this.resultData = CovertData(this.jsonData,idField,fromField, toField); "テスト ストアには列があります":function(){ var month1 = this.resultData[findIdx(this.resultData,'yearmonth',201201)];
var month2 = this.resultData[findIdx(this) .resultData,'yearmonth',201202)];
var month3 = this.resultData[findIdx(this.resultData,'yearmonth',201203)];
var month4 = this.resultData[findIdx(this.resultData)] ,'年月',201204)];
assertEquals(4,this.resultData.length);
assertEquals('23',month1['dfsoft-ppq']);
assertEquals('29',month1['vcom-ppq']) );
assertEquals('33',month2['dfsoft-ppq']);
assertEquals('34',month2['vcom-ppq']); ['dfsoft-ppq']); assertEquals('48',month3['vcom-ppq']);
assertEquals('53',month4['dfsoft-ppq']); assertEquals('52',month4['vcom-ppq']);
assertEquals('27',month1['dfsoft-spq']);
assertEquals('26',month1['vcom-spq']);
assertEquals('38',month2[' dfsoft-spq']);
assertEquals('38',month2['vcom-spq']);
assertEquals('49',month3['dfsoft-spq']); '43',month3['vcom-spq']);
assertEquals('51',month4['dfsoft-spq']);
assertEquals('59',month4['vcom-spq']) );
} })
テストに合格し、変換が成功したことを示します。