How to convert json from one format to another?

后端 未结 1 829
迷失自我
迷失自我 2021-01-16 01:41

I already have a json array like

var a = {
    report: [ 
        {
            Name: \"Nitin\",
            comment: [
                {
                           


        
相关标签:
1条回答
  • 2021-01-16 01:57

    Try like this 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));
    
    0 讨论(0)
提交回复
热议问题