我已经有了像
这样的json数组var a = {
report: [
{
Name: "Nitin",
comment: [
{
count: 0,
mName: "Feb"
},
{
count: 2,
mName: "Mar"
}
],
view: [
{
count: 1,
mName: "Feb"
},
{
count: 0,
mName: "Mar"
}
],
rating: [
{
count: 3,
mName: "Feb"
},
{
count: 2,
mName: "Mar"
}
],
Place: "Vatakara"
},
{
Name: "Sachin",
comment: [
{
count: 3,
mName: "Feb"
},
{
count: 1,
mName: "Mar"
}
],
view: [
{
count: 2,
mName: "Feb"
},
{
count: 1,
mName: "Mar"
}
],
rating: [
{
count: 0,
mName: "Feb"
},
{
count: 3,
mName: "Mar"
}
],
Place: "Mumbai"
}
]
};
我想将此格式的数据更改为
var a = [
{
"Name": "Nithin",
"Month": "Feb",
"viewCount": 1,
"ratingCount": 3,
"commentCount": 0,
"place":"Vatakara"
}, {
"Name": "Nithin",
"Month": "Mar",
"viewCount": 0,
"ratingCount": 2,
"commentCount": 2,
"place":"Vatakara"
},
{
"Name": "Sachin",
"Month": "Feb",
"Views": 2,
"ratingCount": 0,
"commentCount": 3,
"place":"Mumbai"
},
{
"Name": "Sachin",
"Month": "Mar",
"viewCount": 1,
"ratingCount": 3,
"commentCount": 1,
"place":"Mumbai"
}
];
如何使用jquery转换此json?请帮忙DEMO
答案 0 :(得分:4)
尝试这样 JS FIDDLE DEMO
var results = {
report: [{
Name: "Nitin",
comment: [{
count: 0,
mName: "Feb"
}, {
count: 2,
mName: "Mar"
}],
view: [{
count: 1,
mName: "Feb"
}, {
count: 0,
mName: "Mar"
}],
rating: [{
count: 3,
mName: "Feb"
}, {
count: 2,
mName: "Mar"
}],
Place: "Vatakara"
}, {
Name: "Sachin",
comment: [{
count: 3,
mName: "Feb"
}, {
count: 1,
mName: "Mar"
}],
view: [{
count: 2,
mName: "Feb"
}, {
count: 1,
mName: "Mar"
}],
rating: [{
count: 0,
mName: "Feb"
}, {
count: 3,
mName: "Mar"
}],
Place: "Mumbai"
}]
};
/////////////////////////////////////////////////////////////////////
var innArr = [];
for (var i = 0; i < results.report.length; i++) {
for(var j=0;j<results.report[i].comment.length;j++)
{
innArr[innArr.length] = {
"Name": results.report[i].name,
"Month": results.report[i].comment[j].mName,
"comment": results.report[i].comment[j].count,
"rating": results.report[i].rating[j].count,
"view": results.report[i].view[j].count
};
}
}
alert(JSON.stringify(innArr));