问题描述:

I am new in json ,I want create a HighChart,I have data in JSON format and i have converted it into table format [below is data and column extracted from JSON ] .

Venue   DateRange   Clicks

Yahoo Search     2010-04-01   2172

Google                2010-04-01   10830

MSN                    2010-04-01   555

SSP                     2010-04-01   0

Yahoo Search      2010-04-02   1746

Google                 2010-04-02   8508

MSN                     2010-04-02   477

Google                  2010-04-03   5061

Yahoo Search       2010-04-03   1113

MSN                      2010-04-03   192

i want create a JSON string in Following Format.How can i do this

xAxis: {categories: ['2010-04-01', '2010-04-02', ' 2010-04-03']},

series: [{

name: 'Yahoo Search',

data: [2172 ,1746,1113]

},

{

name: 'Google',

data: [10830,8508,5061]

},

{

name: 'MSN',

data: [555,477,792]

},

{

name: 'SSP',

data: [0,0,0]

}]

My json data is here

{"schema":{"fields":[{"name":"NM_Venue","type":"STRING","mode":"NULLABLE"},{"name":"NM_DateRange","type":"STRING","mode":"NULLABLE"},{"name":"M_Clicks","type":"INTEGER","mode":"NULLABLE"}]},"totalRows":"10","rows":[{"f":[{"v":"Yahoo Search"},{"v":"2010-04-01"},{"v":"2172"}]},{"f":[{"v":"Google"},{"v":"2010-04-01"},{"v":"10830"}]},{"f":[{"v":"MSN"},{"v":"2010-04-01"},{"v":"555"}]},{"f":[{"v":"SSP"},{"v":"2010-04-01"},{"v":"0"}]},{"f":[{"v":"Yahoo Search"},{"v":"2010-04-02"},{"v":"1746"}]},{"f":[{"v":"Google"},{"v":"2010-04-02"},{"v":"8508"}]},{"f":[{"v":"MSN"},{"v":"2010-04-02"},{"v":"477"}]},{"f":[{"v":"Google"},{"v":"2010-04-03"},{"v":"5061"}]},{"f":[{"v":"Yahoo Search"},{"v":"2010-04-03"},{"v":"1113"}]},{"f":[{"v":"MSN"},{"v":"2010-04-03"},{"v":"192"}]}],

"result":{"schema":{"fields":[{"name":"NM_Venue","type":"STRING","mode":"NULLABLE"},{"name":"NM_DateRange","type":"STRING","mode":"NULLABLE"},{"name":"M_Clicks","type":"INTEGER","mode":"NULLABLE"}]},"totalRows":"10","rows":[{"f":[{"v":"Yahoo Search"},{"v":"2010-04-01"},{"v":"2172"}]},{"f":[{"v":"Google"},{"v":"2010-04-01"},{"v":"10830"}]},{"f":[{"v":"MSN"},{"v":"2010-04-01"},{"v":"555"}]},{"f":[{"v":"SSP"},{"v":"2010-04-01"},{"v":"0"}]},{"f":[{"v":"Yahoo Search"},{"v":"2010-04-02"},{"v":"1746"}]},{"f":[{"v":"Google"},{"v":"2010-04-02"},{"v":"8508"}]},{"f":[{"v":"MSN"},{"v":"2010-04-02"},{"v":"477"}]},{"f":[{"v":"Google"},{"v":"2010-04-03"},{"v":"5061"}]},{"f":[{"v":"Yahoo Search"},{"v":"2010-04-03"},{"v":"1113"}]},{"f":[{"v":"MSN"},{"v":"2010-04-03"},{"v":"192"}]}]}}

网友答案:

You can convert your existing JSON string to the new JSON string e.g. with this function:

function makeJSONstring(jsonstr) {
    var rows = JSON.parse(jsonstr).rows;
    console.log(rows);
    var data = [];
    for (var i=0; i<rows.length; i++) {
        data.push({
            Venue: rows[i].f[0].v,
            DateRange: rows[i].f[1].v,
            Clicks: rows[i].f[2].v | 0
        });
    }
    //console.log(data);
    var out = {
        xAxis: {
            categories: [ /*'2010-04-01', '2010-04-02', ' 2010-04-03'*/ ]
        },
        series: [ /*{ name: 'Yahoo Search', data: [2172 ,1746,1113] } , etc. */ ]
    };
    var cat = {}, ser = {}; // temporary variables
    for (i = 0; i < data.length; i++) {
        // data[i].Vanue , data[i].DateRange , data[i].Clicks
        if (cat[data[i].DateRange] === undefined) {
            cat[data[i].DateRange] = true;
            out.xAxis.categories.push(data[i].DateRange);
        }
        if (ser[data[i].Venue] === undefined) {
            ser[data[i].Venue] = out.series.length;
            out.series.push({
                name: data[i].Venue,
                data: [data[i].Clicks]
            });
        } else {
            var index = ser[data[i].Venue];
            out.series[index].data.push(data[i].Clicks);
        }
    }
    //console.log(out);
    return JSON.stringify(out);
}

JSFIDDLE

Or alternative example, where data are collected from the table is here: jsfiddle

相关阅读:
Top