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

时间:2014-07-19 09:39:39

标签: javascript jquery json

我有一个喜欢

的json
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
}
];

4 个答案:

答案 0 :(得分:1)

我使用以下代码在没有jQuery的情况下工作。

代码包含注释,因此它非常明显。作为一个特别说明,只要视图,评级和评论在一个条目中具有相同的月份数,即使您对不同的矩阵条目有不同的月数,我的代码也会起作用。我想让它像这样工作,因为它不是硬编码的做事方式。

请参阅 Js fiddle example ,并记得打开开发者控制台查看结果。

代码也在这里,如下:

// 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);

答案 1 :(得分:0)

你走了。

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"
}]

答案 2 :(得分:0)

Fiddle

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);

<强>更新
在增强应用程序的同时,指定数组中的月份和属性可能会为您提供更大的灵活性。

Fiddle

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));

答案 3 :(得分:0)

这应该对你有用

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);