2011-01-10 69 views
0

這是它提供瞭如下結果我的示例應用程序:Java來JSON格式

Array Values : 
arr[0][0] :6 
arr[0][1] :0 
arr[0][2] :0 
arr[1][0] :0 
arr[1][1] :0  
arr[1][2] :0 

因爲我有這樣的jQuery Highcharts:

chart = new Highcharts.Chart({ 
           chart: { 
           renderTo: 'container', 
           defaultSeriesType: 'column' 
           }, 
           title: { 
         text: document.chart.chartTitle.value 
           },            
           series: [{ 
           name: 'USA', 
           data: [50, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] 
           }, { 
           name: 'New York', 
           data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3] 
           }, { 
           name: 'London', 
           data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2] 
           }, { 
           name: 'Berlin', 
           data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1] 
           }] 
         }); 

我必須通過ro_count,ro_free_count等代替「系列」中的'名稱'和'arr [i] [j]'值而不是'數據'位置... JSON格式新增。

任何人都可以幫助我如何轉換和傳遞這些值到這個圖表應用程序。

+1

我假設你的意思是JavaScript而不是Java? – diagonalbatman 2011-01-10 11:24:23

+0

我需要關於如何將java變量值轉換爲json格式的任何示例。我現在清楚了嗎?你可以提供任何示例程序 – Maya 2011-01-11 11:14:57

回答

0

最後,我寫數據的,我需要通過這個圖表應用到一個CSV文件,並將其傳遞給highchart如下:

String CSVFileToWrite = "../../MyCSVFile.csv";//file with .csv extension 
    String colkeys = "categories,jan,oct,nov"; 
    String rowKeys = "row1,row2"; 
    String[] row = rowKeys.split(","); 

    String[][] array = {{"250,340,456"}, {"123,567,789"}}; 
    System.out.print(array[0][0]); 
    System.out.println(""); 
    System.out.print(array[1][0]); 
    try { 
     File file = new File(CSVFileToWrite); 
     FileWriter fw1 = new FileWriter(file); 
     BufferedWriter bw = new BufferedWriter(fw1); 

     int col=0; 
     int row1=3; 
     int i=0; 
     String[][] arrayValues = new String[row1][1]; 
     arrayValues[0][0]=colkeys; 
     for(int r=1; r < row1 ;r++) { 
      arrayValues[r][col]=row[i] + "," + array[i][col]; 
      //System.out.println("ArrayValues["+r+"][" + col +"]" + arrayValues[r][col]); 
      i++; 
     } 
     for(int r=0 ; r < row1 ;r++) { 
      bw.write(arrayValues[r][col]); 
      bw.newLine(); 
      bw.flush(); 
     } 
     bw.close(); 
     System.out.println("File generated Successufully at location "+ file.getAbsolutePath()); 
    } catch (IOException ex) { 
    } 

這將創建一個名爲CSV文件「MyCSVFile.csv」

,我包括在該文件到highcharts使用

$.get('MyCSVFile.csv',function(data) { 
} 

感謝&問候,

Maya