所以我使用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文件像这样输出
&#34; \ n \ nID:ID1 \ n \ n说明:NOTE1&#34;&#34; \ n \ nID:ID2 \ n \ n说明:NOTE2&#34;
添加以下值
ID = ID1说明= NOTE1 ID = ID2说明 - NOTE2
答案 0 :(得分:-1)
要使用Gson序列化数组,请执行此操作。
public void writeFile() throws IOException {
try (Writer writer = new FileWriter("src\\Output.json")) {
Gson gson = new Gson();
gson.toJson(NoteArray, writer);
}
}
}