繁体   English   中英

如何将一种格式的json转换为另一种格式

[英]How to convert json of one format to another format

我有一个像

var UserMatrix =[{
    ID: 1,
    Name: "Sid Edelmann",
    UPI: 20483,
    Guru: "Yes",
    Views: {
        February: 12,
        March: 8,
        April: 10,
        May: 11,
        June: 8
    },
    Ratings: {
        February: 1,
        March: 2,
        April: 0,
        May: 0,
        June: 0
    },
    Comments: {
        February: 1,
        March: 1,
        April: 0,
        May: 0,
        June: 1
    },
    TotalViews: {
        FebJune: 49
    },
    TotalRatings: {
        FebJune: 3
    },
    AverageRatings: {
        FebJune: '#'
    },
    TotalComments: {
        FebJune: 3
    }
}, 
{
    ID: 6,
    Name: "Parthasarathy Perumbali",
    UPI: "999999",
    Guru: "",
    Views: {
        February: "8",
        March: "5",
        April: "4",
        May: "1",
        June: "8"
    },
    Ratings: {
        February: "2",
        March: "1",
        April: "2",
        May: "1",
        June: "2"
    },
    Comments: {
        February: "3",
        March: "0",
        April: "0",
        May: "0",
        June: "0"
    },
    TotalViews: {
        FebJune: "26"
    },
    TotalRatings: {
        FebJune: "8"
    },
    AverageRatings: {
        FebJune: "#"
    },
    TotalComments: {
        FebJune: "3"
    }
}
];

我想将此json转换为以下内容。 我怎样才能做到这一点?

var blogComments = [
{
    "Name": "Sid Edelmann",
    "Month": "Feb",
    "Views": 12,
    "Ratings": 1,
    "Comments": 1
}, {
    "Name": "Sid Edelmann",
    "Month": "Mar",
    "Views": 8,
    "Ratings": 2,
    "Comments": 1
},
{
    "Name": "Sid Edelmann",
    "Month": "Apr",
    "Views": 10,
    "Ratings": 0,
    "Comments": 0
},
{
    "Name": "Sid Edelmann",
    "Month": "May",
    "Views": 11,
    "Ratings": 0,
    "Comments": 0
},
{
    "Name": "Sid Edelmann",
    "Month": "Jun",
    "Views": 8,
    "Ratings": 0,
    "Comments": 1
},
{
    "Name": "Parthasarathy Perumbali",
    "Month": "Feb",
    "Views": 8,
    "Ratings": 2,
    "Comments": 3
},
{
    "Name": "Parthasarathy Perumbali",
    "Month": "Mar",
    "Views": 5,
    "Ratings": 1,
    "Comments": 0
},
{
    "Name": "Parthasarathy Perumbali",
    "Month": "Apr",
    "Views": 4,
    "Ratings": 2,
    "Comments": 0
},
{
    "Name": "Parthasarathy Perumbali",
    "Month": "May",
    "Views": 1,
    "Ratings": 1,
    "Comments": 0
},
{
    "Name": "Parthasarathy Perumbali",
    "Month": "Jun",
    "Views": 8,
    "Ratings": 2,
    "Comments": 0
}
];

我编写了以下代码以在没有jQuery的情况下工作。

该代码包含注释,因此非常不言自明。 需要特别注意的是,即使您对不同的矩阵条目具有不同的月数,只要“视图”,“评分”和“注释”在一个条目中具有相同的月数,我的代码也可以工作。 我想使它像这样工作,因为它不是硬编码的处理方式。

请参阅Js fiddle示例,并记得打开开发人员控制台以查看结果。

代码也在这里,如下:

// UserMatrix data....

var UserMatrix =[{
    ID: 1,
    Name: "Sid Edelmann",
    UPI: 20483,
    Guru: "Yes",
    Views: {
        February: 12,
        March: 8,
        April: 10,
        May: 11,
        June: 8
    },
    Ratings: {
        February: 1,
        March: 2,
        April: 0,
        May: 0,
        June: 0
    },
    Comments: {
        February: 1,
        March: 1,
        April: 0,
        May: 0,
        June: 1
    },
    TotalViews: {
        FebJune: 49
    },
    TotalRatings: {
        FebJune: 3
    },
    AverageRatings: {
        FebJune: '#'
    },
    TotalComments: {
        FebJune: 3
    }
}, 
{
    ID: 6,
    Name: "Parthasarathy Perumbali",
    UPI: "999999",
    Guru: "",
    Views: {
        February: "8",
        March: "5",
        April: "4",
        May: "1",
        June: "8"
    },
    Ratings: {
        February: "2",
        March: "1",
        April: "2",
        May: "1",
        June: "2"
    },
    Comments: {
        February: "3",
        March: "0",
        April: "0",
        May: "0",
        June: "0"
    },
    TotalViews: {
        FebJune: "26"
    },
    TotalRatings: {
        FebJune: "8"
    },
    AverageRatings: {
        FebJune: "#"
    },
    TotalComments: {
        FebJune: "3"
    }
}
];

/** 
 * Yay! Method for converting UserMatrix to blogComments
 *
 */
function convertUserMatrixToBlogComments() {

    // Final format
    var blogComments = [],

    // Current matrix entry
    userMatrix,

    // Months
    months = {};

    // Loop each object in UserMatrix
    for(var i=0; i < UserMatrix.length; i++) {

        // Current
        userMatrix = UserMatrix[i];

        // Find out months
        for (var m in userMatrix.Views) { 
            if(userMatrix.Views.hasOwnProperty(m)) {
                // Makes container for months
                // e.g. February: "Feb"
                months[m] = m.substring(0, 3);
            }
        };

        // Go through all matrix data for months and push to comments
        for(var j in months) {
            if(months.hasOwnProperty(j)) {

                 blogComments.push({
                    Name: userMatrix.Name,
                    Month: months[j],
                    Views: parseInt(userMatrix.Views[j], 10),
                    Ratings: parseInt(userMatrix.Ratings[j], 10),
                    Comments: parseInt(userMatrix.Comments[j], 10)
                }); 
            }   
        }   

        // Next cycle starts here..
        months = {};

    }

    // We are done!
    return blogComments;

}

// Lets do this!
var blogComments = convertUserMatrixToBlogComments();

// See the results
console.log(blogComments);

干得好。

newUsers = [];
$.each(UserMatrix, function (i, user) {
    $.each(user.Views, function(key, value){
        newUser = {};
        newUser['Name'] = user['Name'];
        newUser['Month'] = key;
        newUser['Views'] = value;
        newUser['Ratings'] = user.Ratings[key];
        newUser['Comments'] = user.Comments[key];
        newUsers.push(newUser);
    });
});
    console.log(JSON.stringify(newUsers));

演示: http//jsfiddle.net/robschmuecker/Bc4hw/

输出:

[{
    "Name": "Sid Edelmann",
    "Month": "February",
    "Views": 12,
    "Ratings": 1,
    "Comments": 1
}, {
    "Name": "Sid Edelmann",
    "Month": "March",
    "Views": 8,
    "Ratings": 2,
    "Comments": 1
}, {
    "Name": "Sid Edelmann",
    "Month": "April",
    "Views": 10,
    "Ratings": 0,
    "Comments": 0
}, {
    "Name": "Sid Edelmann",
    "Month": "May",
    "Views": 11,
    "Ratings": 0,
    "Comments": 0
}, {
    "Name": "Sid Edelmann",
    "Month": "June",
    "Views": 8,
    "Ratings": 0,
    "Comments": 1
}, {
    "Name": "Parthasarathy Perumbali",
    "Month": "February",
    "Views": "8",
    "Ratings": "2",
    "Comments": "3"
}, {
    "Name": "Parthasarathy Perumbali",
    "Month": "March",
    "Views": "5",
    "Ratings": "1",
    "Comments": "0"
}, {
    "Name": "Parthasarathy Perumbali",
    "Month": "April",
    "Views": "4",
    "Ratings": "2",
    "Comments": "0"
}, {
    "Name": "Parthasarathy Perumbali",
    "Month": "May",
    "Views": "1",
    "Ratings": "1",
    "Comments": "0"
}, {
    "Name": "Parthasarathy Perumbali",
    "Month": "June",
    "Views": "8",
    "Ratings": "2",
    "Comments": "0"
}]

小提琴

var finalArr = [];
var months = ["February", "March", "April", "May", "June"];
UserMatrix.forEach(function (user) {
    months.forEach(function (m) {
        finalArr.push({
            Name: user.Name,
            Month: m,
            Views: user.Views[m],
            Ratings: user.Ratings[m],
            Comments: user.Comments[m]
        });
    });
});
document.getElementById('op').innerHTML = JSON.stringify(finalArr);
console.log(finalArr);

更新
在数组中指定月份和属性可以在增强应用程序的同时提供更大的灵活性。

小提琴

var finalArr = [];
var months = ["February", "March", "April", "May", "June"];
var attr = ["Views", "Ratings", "Comments"];
UserMatrix.forEach(function (user) {
    months.forEach(function (m) {
        var newObj = {};
        newObj.Name=user.Name;
        newObj.Month = m;
        attr.forEach(function (a) {
            newObj[a]=user[a][m];
        });
        finalArr.push(newObj);
    });
});
document.getElementById('op').innerHTML = JSON.stringify(finalArr);
console.log(JSON.stringify(finalArr));

这应该为你工作

var result = [];

UserMatrix.forEach(function (user) {
    var allMonths = Object.keys(user.Views);
    allMonths.forEach(function(month){
        var monthObject = {};
        monthObject["Name"] = user.Name;
        monthObject["Month"] = month.slice(0,3);
        monthObject["Views"] = user.Views[month];
        monthObject["Ratings"] = user.Ratings[month];
        monthObject["Comments"] = user.Comments[month];
        result.push(monthObject);
    });
})

console.log(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM