java - How to generate random number and export it as JSON? -
i planning generate random number , export json using json-simple
. have following code
public class main implements jsonaware{ private final int data; public main(int data){ this.data = data; } public string tojsonstring(){ stringbuffer sb = new stringbuffer(); sb.append("["); sb.append(data); sb.append("]"); sb.append(","); return sb.tostring(); } public static void main(string[] args){ jsonarray da = new jsonarray(); random generator = new random(); int [][] grid; grid = new int[128][14]; (int row = 0; row < 128; row++){ (int col = 0; col < 14; col++){ grid[row][col] = generator.nextint(100); // da.add("%d",grid[row][col]); da.add(grid[row][col]); } // system.out.println(); } system.out.println(da); }
this prints following:
[9,62,6,60,29,28,59,56,67,61,53,23,22,31,15,96,94,85,65,94,15,7,91,...]
but trying print
[9,62,6,60,29,28,59,56,67,61,53,23,22,31,15,96,94,85,65,94,15,7,91,...], [68,48,40,44,47,85,8,43,17,78,35,7,77,26,80,34,19,47,17,0,59,5,79,...], . . .
can tell me how split row column?
as far understood problem, want 128 rows separate jsonarray i.e. 1 row = 1 jsonarray.
:
public static void main(string[] args){ jsonarray jarray = new jsonarray(); random generator = new random(); int [][] grid; grid = new int[128][14]; (int row = 0; row < 128; row++){ jsonarray da = new jsonarray(); (int col = 0; col < 14; col++){ grid[row][col] = generator.nextint(100); da.add(grid[row][col]); } jarray.add(da); } system.out.println(jarray); }
Comments
Post a Comment