function Covertdata (Jsondata,idfield, Fromfield, Tofield) {
var result = [], currecord =null, num;
var fromfields = Fromfield.split (', ');
Loops the entire array: [{...},{...},{},...]
for (Var idx=0;idx<jsondata.length;idx++) {
num = Findidx (result, IDfield, Jsondata[idx][idfield]);
if (num!=-1) {
Currecord = Result[num];
}
else{
Currecord = {};
};
Looping the fields in each JSON object
for (var key in Jsondata[idx]) {
Processing the transformed data content
for (Var i=0;i<fromfields.length;i++) {
if (key = = Fromfields[i]) {
currecord[jsondata[idx][tofield]+ '-' + fromfields[i]] = Jsondata[idx][key];
Break
}
}
Only the Identity field data is processed except for the content of the data
if (key = = IDfield) {
Currecord[key] = Jsondata[idx][key];
}
}
if (num==-1) {
Result.push (Currecord);
}
}
return result;
}
Function Findidx (jsondata, ColumnName, value) {
for (var idx = 0;idx<jsondata.length;idx++) {
if (jsondata[ Idx][columnname]==value)
return idx;
}
Return-1; The test code for the
}
Jstestdriver is as follows:
TestCase ("Test JSON data row to column", {
Setup:function () {
This.jsondata = [{ Yearmonth:201201,ppq:23,spq:27,company: ' Dfsoft '},
{yearmonth:201202,ppq:33,spq:38,company: ' Dfsoft '},
{ Yearmonth:201203,ppq:43,spq:49,company: ' Dfsoft '},
{yearmonth:201204,ppq:53,spq:51,company: ' Dfsoft '},
{ Yearmonth:201201,ppq:29,spq:26,company: ' vcom '},
{yearmonth:201202,ppq:34,spq:38,company: ' vcom '},
{ Yearmonth:201203,ppq:48,spq:43,company: ' vcom '},
{yearmonth:201204,ppq:52,spq:59,company: ' vcom '}];
var Fromfield = ' Ppq,spq ', Tofield = ' Company ', IDfield = ' yearmonth ';
This.resultdata = Covertdata (This.jsondata,idfield,fromfield, Tofield);
},
"Test store has columns": 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, ' Yearmonth ', 201204)];
Assertequals (4,this.resultdata.length);
Assertequals (' month1[', ' DFSOFT-PPQ ']);
Assertequals (' o ', month1[' VCOM-PPQ ');
Assertequals (' month2[', ' DFSOFT-PPQ ']);
Assertequals (' month2[', ' VCOM-PPQ ']);
Assertequals (' month3[', ' DFSOFT-PPQ ']);
Assertequals (' month3[', ' VCOM-PPQ ']);
Assertequals (' n ', month4[' DFSOFT-PPQ ')];
Assertequals (' month4[', ' VCOM-PPQ ']);
Assertequals (' A ', month1[' DFSOFT-SPQ ');
Assertequals (' num ', month1[' VCOM-SPQ '));
Assertequals (' month2[', ' DFSOFT-SPQ ']);
Assertequals (' month2[', ' VCOM-SPQ ']);
Assertequals (' month3[', ' DFSOFT-SPQ ']);
Assertequals (' month3[', ' VCOM-SPQ ']);
Assertequals (' Wuyi ', month4[' DFSOFT-SPQ ']);
Assertequals (' month4[', ' VCOM-SPQ ']);
}
})
The test passed, indicating that the conversion was successful.