因此,我正在使用gson libary将用户输入的数组输出到.json文件,我想知道如何以某种方式对其进行编码,以便它在没有注释的情况下输出信息使.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 Programn" +
"4. Write Filen" +
"5. Test coden" +
"----------------------------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 withnn");
String inputIDnote = Postit.menu.nextLine();
System.out.println("Enter your notenn");
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 "nn" + "ID: " + inputIDnote + "nn" + " Description: " +
noteDescription;
}
}
.json文件像以下那样输出
" n nid:id1 n n描述:note1" n nid:id2 n n描述:note2:note2"
添加了以下值
id = id1描述= note1ID = ID2说明-Note2
使用gson序列化您的数组。
public void writeFile() throws IOException {
try (Writer writer = new FileWriter("src\Output.json")) {
Gson gson = new Gson();
gson.toJson(NoteArray, writer);
}
}
}