所以我現在用的是GSON Libary輸出我的用戶輸入陣列到以.json文件,我在想,所以它輸出信息來源沒有註釋,使其在某種程度上我怎麼代碼時輸出更容易將.json文件導入到數組中?如何改變陣列
我將展示的代碼,以及如何陣列輸出,因此更有意義。
Postit.java
package postit;
import java.util.Scanner;
class Postit {
public static Scanner menu = new Scanner(System.in);
public static void main(String[] args) throws Exception {
int MenuOption = 0;
NewStorage G = new NewStorage(); // Case 1 Object
while(MenuOption != 3){
System.out.println(
"\n--------Note System-------\n" +
"----------------------------\n" +
"1. Create a Note \n" +
"2. View Notes \n" +
"3. Close Program\n" +
"4. Write File\n" +
"5. Test code\n" +
"----------------------------\n");
MenuOption = menu.nextInt();
menu.nextLine();
switch (MenuOption) {
case 1:
G.printinfo();
G.Notestore();
break;
case 2:
G.viewNotes();
G.printNotes();
break;
case 3:
System.out.println("Program is closing");
System.exit(0);
break;
case 4:
G.writeFile();
System.out.println("Done.");
break;
case 5:
G.Gsontest();
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
NewStorage.java
package postit;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class NewStorage {
Gson gson = new Gson();
ArrayList<Note> NoteArray = new ArrayList<Note>(20);
public void printinfo() {
System.out.println("--- Fill note here ---");
}
public void Gsontest() {
String userJson = gson.toJson(NoteArray.toString());
gson.toJson(userJson, System.out);
}
public void Notestore() {
System.out.println("Enter the note ID you wish to attach the note with\n\n");
String inputIDnote = Postit.menu.nextLine();
System.out.println("Enter your note\n\n");
String noteDescription = Postit.menu.nextLine();
NoteArray.add(new Note(inputIDnote, noteDescription));
}
public void viewNotes() {
System.out.println("Please enter the number of the note you wish to view.");
int count = 0;
for (int i = 0; i < NoteArray.size(); i++) {
System.out.println((count++) + ": " + NoteArray.get(i).inputIDnote);
}
}
public void printNotes() {
int count = Postit.menu.nextInt();
Postit.menu.nextLine();
System.out.println(count + " " + NoteArray.get(count));
}
public void writeFile() throws IOException {
try
(Writer writer = new FileWriter("src\\Output.json"))
{
Gson gson = new GsonBuilder().create();
for (int i = 0; i < NoteArray.size(); i++) {
gson.toJson(NoteArray.get(i).toString(), writer);
}
writer.close();
}
}
}
note.java
package postit;
class Note {
String inputIDnote;
String noteDescription;
public Note(String inputIDnote, String noteDescription) {
this.inputIDnote = inputIDnote;
this.noteDescription = noteDescription;
}
@Override
public String toString() {
return "\n\n" + "ID: " + inputIDnote + "\n\n" + " Description: " +
noteDescription;
}
}
的上傳.json文件被輸出這樣
「\ n \ nID:ID1 \ n \ n描述:NOTE1「 「\ n \的NID:ID2 \ n \ n說明:注2:」
用下面的值添加
ID = ID1描述= NOTE1 ID = ID2描述 - 注2:
你不應該在你的NoteArray在這裏調用toString()這裏String userJson = gson.toJson(NoteArray.toString());只需將該實例傳遞給toJson()方法即可。 – bhspencer
你指的是writefile函數嗎? – DarkEvE
好吧,我改了行這 GSON GSON =新GsonBuilder()創建()。 字符串userJson = gson.toJson(NoteArray.toString()); 對(INT I = 0; I
DarkEvE