我試圖以下列格式顯示JSon文件。但是當我運行我的程序時,它顯示底部的頂部。爲什麼是這樣?我是否正確地做了這件事?請在代碼下面找到輸出和預期輸出的圖片。Json格式顯示錯誤
代碼:
public class Student {
public static void main(String[] args) {
int a = -7;
int b = 7;
int k = 103;
int order = 109;
int px = 60;
int py = 76;
int qx = 101;
int qy = 42;
int n = 3;
int rx = 2;
int ry = 102;
int sx = 64;
int sy = 17;
JSONObject obj = new JSONObject();
obj.put("name", "JEAN-LUC PALMYRE");
obj.put("srn", "120299364");
JSONObject objEcc = new JSONObject();
objEcc.put("a",a);
objEcc.put("b",b);
objEcc.put("k",k);
objEcc.put("order",order);
obj.put("ecc", objEcc);
JSONObject objModk_add = new JSONObject();
objModk_add.put("x", px);
objModk_add.put("y", py);
obj.put("p", objModk_add);
objModk_add.put("x", qx);
objModk_add.put("y", qy);
obj.put("q", objModk_add);
objModk_add.put("x", rx);
objModk_add.put("y", ry);
obj.put("r", objModk_add);
JSONObject objModk_mul = new JSONObject();
objModk_mul.put("x", px);
objModk_mul.put("y", py);
obj.put("p", objModk_mul);
try (FileWriter file = new FileWriter("Jean-LucPalmyre_120299364_CO3326_cw1.json"))
{
file.write(obj.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(obj);
}
}
輸出:
預期輸出:
謝謝!將嘗試一下 –