如何使用 Javascript 将 JSON 格式转换为另一种格式?
How convert a JSON format to another using Javascript?
我正在使用 Oracle JET.The 实现日期选择器 JET.The JET cookbook 为我提供了具有 JSON 文件的示例数据选择器,如下所示。
"Holidays": {
"*": {
"1": {
"14": {
"className": "holiday"
},
"26": {
"className": "holiday"
}
}
}
而 Json 我从休息服务中获得的文件如下
[
{
"YEAR":"2017",
"MONTH":"1",
"DAY":[
{
"N_DAYS":"25",
"className":"holiday"
},
{
"N_DAYS":"25",
"className":"holiday"
}
]
},
];
我如何转换 JSON 我从其他服务获得的文件,如 JET cookbook
中提供的上述服务
myStuff = [
{
"YEAR":"2017",
"MONTH":"1",
"DAY":[
{
"N_DAYS":"14",
"className":"holiday"
},
{
"N_DAYS":"26",
"className":"holiday"
}
]
},
];
Holidays = {
'*': { }
}
for (var i = 0; i < myStuff.length; i++) {
var month = myStuff[i].MONTH;
for (var j = 0; j < myStuff[i].DAY.length; j++) {
if (typeof Holidays['*'][myStuff[i].MONTH] == 'undefined') {
Holidays['*'][month] = {};
}
Holidays['*'][myStuff[i].MONTH][myStuff[i].DAY[j].N_DAYS] = {
className: myStuff[i].DAY[j].className
}
}
}
Holidays = { Holidays: Holidays }
console.log(Holidays);
这是 fiddle link
https://fiddle.jshell.net/q80212b7/1/
var myStuff= [ {
"YYYY_YEAR" : "2013",
"MMM_MONTH" : "06",
"DAY_OUT" : {
"DAY_OUT_ITEM" : {
"N_DAYS" : "06",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Lalat Al Miraj"
}
}
}, {
"YYYY_YEAR" : "2017",
"MMM_MONTH" : "06",
"DAY_OUT" : {
"DAY_OUT_ITEM" : [ {
"N_DAYS" : "06",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Lalat Al Miraj"
} ]
}
}, {
"YYYY_YEAR" : "2013",
"MMM_MONTH" : "01",
"DAY_OUT" : {
"DAY_OUT_ITEM" : [ {
"N_DAYS" : "01",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "New Year's Day"
}, {
"N_DAYS" : "24",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Milad Un Nabi"
} ]
}
}, {
"YYYY_YEAR" : "2017",
"MMM_MONTH" : "01",
"DAY_OUT" : {
"DAY_OUT_ITEM" : [ {
"N_DAYS" : "01",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "New Year's Day"
}, {
"N_DAYS" : "24",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Milad Un Nabi"
} ]
}
}];
Holidays = {
'value': { }
}
for (var i = 0; i < myStuff.length; i++) {
var year = parseInt(myStuff[i].YYYY_YEAR,10);
var month = parseInt(myStuff[i].MMM_MONTH,10);
if (typeof Holidays['value'][myStuff[i].YYYY_YEAR] == 'undefined') {
Holidays['value'][year] = {};
}
if (typeof Holidays['value'][year][myStuff[i].MMM_MONTH] == 'undefined') {
Holidays['value'][year][month] = {};
}
if(myStuff[i].DAY_OUT.DAY_OUT_ITEM instanceof Array) {
for (var j = 0; j < myStuff[i].DAY_OUT.DAY_OUT_ITEM.length; j++) {
var day=parseInt(myStuff[i].DAY_OUT.DAY_OUT_ITEM[j].N_DAYS,10);
Holidays['value'][year][month][day] = {
className: myStuff[i].DAY_OUT.DAY_OUT_ITEM[j].CLASSNAME,
tooltip: myStuff[i].DAY_OUT.DAY_OUT_ITEM[j].TOOLTIP
};
}
}
else
{
var day=parseInt(myStuff[i].DAY_OUT.DAY_OUT_ITEM.N_DAYS,10);
Holidays['value'][year][month][day] = {
className: myStuff[i].DAY_OUT.DAY_OUT_ITEM.CLASSNAME,
tooltip: myStuff[i].DAY_OUT.DAY_OUT_ITEM.TOOLTIP
};
}
}
Holidays = { Holidays: Holidays }
console.log(JSON.stringify(Holidays.Holidays));
我正在使用 Oracle JET.The 实现日期选择器 JET.The JET cookbook 为我提供了具有 JSON 文件的示例数据选择器,如下所示。
"Holidays": {
"*": {
"1": {
"14": {
"className": "holiday"
},
"26": {
"className": "holiday"
}
}
}
而 Json 我从休息服务中获得的文件如下
[
{
"YEAR":"2017",
"MONTH":"1",
"DAY":[
{
"N_DAYS":"25",
"className":"holiday"
},
{
"N_DAYS":"25",
"className":"holiday"
}
]
},
];
我如何转换 JSON 我从其他服务获得的文件,如 JET cookbook
中提供的上述服务myStuff = [
{
"YEAR":"2017",
"MONTH":"1",
"DAY":[
{
"N_DAYS":"14",
"className":"holiday"
},
{
"N_DAYS":"26",
"className":"holiday"
}
]
},
];
Holidays = {
'*': { }
}
for (var i = 0; i < myStuff.length; i++) {
var month = myStuff[i].MONTH;
for (var j = 0; j < myStuff[i].DAY.length; j++) {
if (typeof Holidays['*'][myStuff[i].MONTH] == 'undefined') {
Holidays['*'][month] = {};
}
Holidays['*'][myStuff[i].MONTH][myStuff[i].DAY[j].N_DAYS] = {
className: myStuff[i].DAY[j].className
}
}
}
Holidays = { Holidays: Holidays }
console.log(Holidays);
这是 fiddle link https://fiddle.jshell.net/q80212b7/1/
var myStuff= [ {
"YYYY_YEAR" : "2013",
"MMM_MONTH" : "06",
"DAY_OUT" : {
"DAY_OUT_ITEM" : {
"N_DAYS" : "06",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Lalat Al Miraj"
}
}
}, {
"YYYY_YEAR" : "2017",
"MMM_MONTH" : "06",
"DAY_OUT" : {
"DAY_OUT_ITEM" : [ {
"N_DAYS" : "06",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Lalat Al Miraj"
} ]
}
}, {
"YYYY_YEAR" : "2013",
"MMM_MONTH" : "01",
"DAY_OUT" : {
"DAY_OUT_ITEM" : [ {
"N_DAYS" : "01",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "New Year's Day"
}, {
"N_DAYS" : "24",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Milad Un Nabi"
} ]
}
}, {
"YYYY_YEAR" : "2017",
"MMM_MONTH" : "01",
"DAY_OUT" : {
"DAY_OUT_ITEM" : [ {
"N_DAYS" : "01",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "New Year's Day"
}, {
"N_DAYS" : "24",
"CLASSNAME" : "Holiday",
"TOOLTIP" : "Milad Un Nabi"
} ]
}
}];
Holidays = {
'value': { }
}
for (var i = 0; i < myStuff.length; i++) {
var year = parseInt(myStuff[i].YYYY_YEAR,10);
var month = parseInt(myStuff[i].MMM_MONTH,10);
if (typeof Holidays['value'][myStuff[i].YYYY_YEAR] == 'undefined') {
Holidays['value'][year] = {};
}
if (typeof Holidays['value'][year][myStuff[i].MMM_MONTH] == 'undefined') {
Holidays['value'][year][month] = {};
}
if(myStuff[i].DAY_OUT.DAY_OUT_ITEM instanceof Array) {
for (var j = 0; j < myStuff[i].DAY_OUT.DAY_OUT_ITEM.length; j++) {
var day=parseInt(myStuff[i].DAY_OUT.DAY_OUT_ITEM[j].N_DAYS,10);
Holidays['value'][year][month][day] = {
className: myStuff[i].DAY_OUT.DAY_OUT_ITEM[j].CLASSNAME,
tooltip: myStuff[i].DAY_OUT.DAY_OUT_ITEM[j].TOOLTIP
};
}
}
else
{
var day=parseInt(myStuff[i].DAY_OUT.DAY_OUT_ITEM.N_DAYS,10);
Holidays['value'][year][month][day] = {
className: myStuff[i].DAY_OUT.DAY_OUT_ITEM.CLASSNAME,
tooltip: myStuff[i].DAY_OUT.DAY_OUT_ITEM.TOOLTIP
};
}
}
Holidays = { Holidays: Holidays }
console.log(JSON.stringify(Holidays.Holidays));