Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改阵列的输出方式_Java_Arrays_Json - Fatal编程技术网

Java 更改阵列的输出方式

Java 更改阵列的输出方式,java,arrays,json,Java,Arrays,Json,因此,我使用GSON库将用户输入的数组输出到.json文件,我想知道如何对其进行编码,使其在不添加注释的情况下输出信息,从而更容易将.json文件导入到数组中 我将展示代码以及数组是如何输出的,这样会更有意义 posit.java package postit; import java.util.Scanner; class Postit { public static Scanner menu = new Scanner(System.in); public static void mai

因此,我使用GSON库将用户输入的数组输出到.json文件,我想知道如何对其进行编码,使其在不添加注释的情况下输出信息,从而更容易将.json文件导入到数组中

我将展示代码以及数组是如何输出的,这样会更有意义

posit.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();
        }
    }
}
.json文件的输出方式如下

“\n\nID:ID1\n\n描述:NOTE1”“\n\nID:ID2\n\n描述:NOTE2”

添加以下值

ID=ID1说明=NOTE1
ID=ID2 Description-NOTE2要使用Gson序列化阵列,请执行此操作

public void writeFile() throws IOException {
    try (Writer writer = new FileWriter("src\\Output.json")) {
            Gson gson = new Gson();
            gson.toJson(NoteArray, writer);
        }
    }
}

您不应该在您的NoteArray上调用toString(),这里字符串userJson=gson.toJson(NoteArray.toString());只需将实例传递给toJson()方法。您是指writefile函数吗?我将行更改为Gson Gson=new GsonBuilder().create();字符串userJson=gson.toJson(NoteArray.toString());对于(int i=0;ipublic void writeFile() throws IOException { try (Writer writer = new FileWriter("src\\Output.json")) { Gson gson = new Gson(); gson.toJson(NoteArray, writer); } } }