javascript - How to convert json from one format to another? - Stack Overflow

I already have a json array like var a = {report: [ {Name: "Nitin",ment: [{count: 0,mName: &q

I already have a json array like

var a = {
    report: [ 
        {
            Name: "Nitin",
            ment: [
                {
                    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",
            ment: [
                {
                    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"
        }
    ]
};

I want to change this format of array to

var a = [
{
    "Name": "Nithin",
    "Month": "Feb",
    "viewCount": 1,
    "ratingCount": 3,
    "mentCount": 0,
    "place":"Vatakara"
}, {
    "Name": "Nithin",
    "Month": "Mar",
    "viewCount": 0,
    "ratingCount": 2,
    "mentCount": 2,
    "place":"Vatakara"
},
{
    "Name": "Sachin",
    "Month": "Feb",
    "Views": 2,
    "ratingCount": 0,
    "mentCount": 3,
    "place":"Mumbai"
},
{
    "Name": "Sachin",
    "Month": "Mar",
    "viewCount": 1,
    "ratingCount": 3,
    "mentCount": 1,
    "place":"Mumbai"
}
];

How can I convert this json using jquery?? Please help DEMO

I already have a json array like

var a = {
    report: [ 
        {
            Name: "Nitin",
            ment: [
                {
                    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",
            ment: [
                {
                    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"
        }
    ]
};

I want to change this format of array to

var a = [
{
    "Name": "Nithin",
    "Month": "Feb",
    "viewCount": 1,
    "ratingCount": 3,
    "mentCount": 0,
    "place":"Vatakara"
}, {
    "Name": "Nithin",
    "Month": "Mar",
    "viewCount": 0,
    "ratingCount": 2,
    "mentCount": 2,
    "place":"Vatakara"
},
{
    "Name": "Sachin",
    "Month": "Feb",
    "Views": 2,
    "ratingCount": 0,
    "mentCount": 3,
    "place":"Mumbai"
},
{
    "Name": "Sachin",
    "Month": "Mar",
    "viewCount": 1,
    "ratingCount": 3,
    "mentCount": 1,
    "place":"Mumbai"
}
];

How can I convert this json using jquery?? Please help DEMO

Share Improve this question edited Aug 6, 2014 at 10:40 Nithin Viswanathan asked Aug 5, 2014 at 17:02 Nithin ViswanathanNithin Viswanathan 3,2837 gold badges45 silver badges85 bronze badges 7
  • you can use each – Claudio Redi Commented Aug 5, 2014 at 17:04
  • 3 The first is not JSON. JSON format requires keys to be double-quoted. – Casey Falk Commented Aug 5, 2014 at 17:05
  • Also, what have you tried, bud? :) – Casey Falk Commented Aug 5, 2014 at 17:06
  • @CaseyFalk - jsfiddle/R4jnJ/3 – Nithin Viswanathan Commented Aug 5, 2014 at 17:07
  • 1 @NiThin: Update your question, please. – Casey Falk Commented Aug 5, 2014 at 17:08
 |  Show 2 more ments

1 Answer 1

Reset to default 4

Try like this JS FIDDLE DEMO

var results = {
    report: [{
        Name: "Nitin",
        ment: [{
            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",
        ment: [{
            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].ment.length;j++)
            {
                innArr[innArr.length] = { 
                    "Name": results.report[i].name, 
                    "Month": results.report[i].ment[j].mName, 
                    "ment": results.report[i].ment[j].count, 
                    "rating": results.report[i].rating[j].count, 
                    "view": results.report[i].view[j].count 
                }; 

            }

        } 
alert(JSON.stringify(innArr));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745113478a4611985.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信