【问题标题】:changing how the array is outputted [duplicate]改变数组的输出方式[重复]
【发布时间】:2017-06-01 14:41:53
【问题描述】:

所以我正在使用 GSON 库将用户输入的数组输出到 .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 描述:NOTE2"

添加以下值

ID = ID1 描述 = NOTE1 ID = ID2 描述 - NOTE2

【问题讨论】:

  • 你不应该在这里调用你的 NoteArray 上的 toString() String userJson = gson.toJson(NoteArray.toString());只需将实例传递给 toJson() 方法即可。
  • 你指的是writefile函数吗?
  • 好吧,我把这条线改成了这个 Gson gson = new GsonBuilder().create();字符串 userJson = gson.toJson(NoteArray.toString()); for (int i = 0; i
  • 到处移除 toString() 。使用 Gson 时不需要 toString()。
  • Jarrod Roberson 您已将此标记为重复的问题,该问题没有接受的回答者并且可以说是在问另一个问题。

标签: java arrays json


【解决方案1】:

要使用 Gson 序列化您的数组,请执行此操作。

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

【讨论】:

  • 投反对票的原因是什么?
  • 我知道,这段代码似乎输出了我正在寻找的内容。感谢您的回答。
  • 我不确定 Jarrod Roberson 是否真的阅读了他标记为与此问题重复的问题。那好吧。很高兴我能帮上忙。
猜你喜欢
  • 2014-02-28
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-05
相关资源
最近更新 更多