简单的Java问卷使用JSON

发布于 2025-01-28 11:36:01 字数 2255 浏览 2 评论 0原文

我做了一份简单的问卷。我需要编写对JSON文件的响应。怎么做? 我使用Idea Intellij和Library Gson与JSON合作。 这是主要类“ Quiz”:

package questions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Scanner;

public class Quiz {
    public static <Outfile> void main(String[] args) throws IOException {

        String userJson = "[{\"question\": \"1.What is your marital status?\", \"a\": \"(a)Single\", \"b\": \"(b)Married\"}," +
                "{\"question\": \"2.Are you planning on getting married next year?\", \"a\": \"(a)Yes\", \"b\": \"(b)No\"},"+
                "{\"question\": \"3.How long have you been married?\", \"a\": \"(a)Less than a year\", \"b\": \"(b)More than a year\"},"+
                "{\"question\": \"4.Have you celebrated your one year anniversary?\", \"a\": \"(a)Yes\" , \"b\": \"(b)No\"}]";

        Gson gson = new Gson();
        Type questionListType = new TypeToken<ArrayList<Question>>(){}.getType();

        ArrayList<Question> questionArray = gson.fromJson(userJson, questionListType);

        FileWriter fileWriter = new FileWriter("answer.json");


        for(Question question : questionArray) {
            System.out.println("Question:" + question.question);
            System.out.println("Answer:" + question.a + "   " + question.b);
            Scanner keyboardInput = new Scanner(System.in);
            String answer = keyboardInput.nextLine();
            System.out.println("You got: " + answer);

        }
        
    }}

这是类“问题”:

package questions;
public class Question {

    public String question;
    public String a;
    public String b;

    public String getQuestion() {
        return question;
    }
    public void setQuestion(String question) {
        this.question = question;
    }

    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }
    public void setAnswer2(String b) {
        this.b = b;
    }
  }

我正在尝试使用文件作者,但是我做错了。输出文件为空。请帮我,请

I made a simple questionnaire. I need to write the responses to a JSON file. How to do it?
I use IDEA Intellij and library GSON for work with JSON.
This is main class "Quiz":

package questions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.GsonBuilder;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Scanner;

public class Quiz {
    public static <Outfile> void main(String[] args) throws IOException {

        String userJson = "[{\"question\": \"1.What is your marital status?\", \"a\": \"(a)Single\", \"b\": \"(b)Married\"}," +
                "{\"question\": \"2.Are you planning on getting married next year?\", \"a\": \"(a)Yes\", \"b\": \"(b)No\"},"+
                "{\"question\": \"3.How long have you been married?\", \"a\": \"(a)Less than a year\", \"b\": \"(b)More than a year\"},"+
                "{\"question\": \"4.Have you celebrated your one year anniversary?\", \"a\": \"(a)Yes\" , \"b\": \"(b)No\"}]";

        Gson gson = new Gson();
        Type questionListType = new TypeToken<ArrayList<Question>>(){}.getType();

        ArrayList<Question> questionArray = gson.fromJson(userJson, questionListType);

        FileWriter fileWriter = new FileWriter("answer.json");


        for(Question question : questionArray) {
            System.out.println("Question:" + question.question);
            System.out.println("Answer:" + question.a + "   " + question.b);
            Scanner keyboardInput = new Scanner(System.in);
            String answer = keyboardInput.nextLine();
            System.out.println("You got: " + answer);

        }
        
    }}

This is class "Question":

package questions;
public class Question {

    public String question;
    public String a;
    public String b;

    public String getQuestion() {
        return question;
    }
    public void setQuestion(String question) {
        this.question = question;
    }

    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }
    public void setAnswer2(String b) {
        this.b = b;
    }
  }

I'm trying to use FileWriter, but I'm doing it wrong. The output file is empty. Help me, pls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2025-02-04 11:36:01

您已经创建了一个文件作者,但是实际上您从未为文件写任何东西。这就是为什么文件为空的原因。

  string jsonData = "{}";
  FileWriter output = new FileWriter("answer.json");
  // Writes the string to the file
  output.write(jsonData);

  // Closes the writer
  output.close();

这将使您可以将内容写入文件。在编写内容后,请确保在文件作者上调用CLOSE()。之后,您的文件wonse.json应包含:

{}

You have created a FileWriter, but you are never actually writing anything to the file. This is why the file is empty.

  string jsonData = "{}";
  FileWriter output = new FileWriter("answer.json");
  // Writes the string to the file
  output.write(jsonData);

  // Closes the writer
  output.close();

This would allow you to write contents to a file. Make sure to call close() on the FileWriter after writing your content. After that your file answer.json should contain:

{}
ゝ杯具 2025-02-04 11:36:01
@Test
public void test1() {
    try {
        String userJson = "[{\"question\": \"1.What is your marital status?\", \"a\": \"(a)Single\", \"b\": \"(b)Married\"}," +
                "{\"question\": \"2.Are you planning on getting married next year?\", \"a\": \"(a)Yes\", \"b\": \"(b)No\"}," +
                "{\"question\": \"3.How long have you been married?\", \"a\": \"(a)Less than a year\", \"b\": \"(b)More than a year\"}," +
                "{\"question\": \"4.Have you celebrated your one year anniversary?\", \"a\": \"(a)Yes\" , \"b\": \"(b)No\"}]";

        JSONArray jsonArray = JSON.parseArray(userJson);
        System.out.println(jsonArray);
        List<Question> javaList = jsonArray.toJavaList(Question.class);
        writeToFile(javaList);
    } catch (Exception e) {
        e.printStackTrace();
    }


}

private void writeToFile(List<Question> data) throws Exception {
    BufferedWriter bw;
    try {
        bw = new BufferedWriter(new FileWriter("D:\\test.txt"));
        for (int i = 0; i < data.size(); i++) {
            bw.write(JSON.toJSONString(data.get(i)));
            bw.newLine();
            bw.flush();
        }
        bw.close();
    } catch (Exception e) {
        throw e;
    }
}
@Test
public void test1() {
    try {
        String userJson = "[{\"question\": \"1.What is your marital status?\", \"a\": \"(a)Single\", \"b\": \"(b)Married\"}," +
                "{\"question\": \"2.Are you planning on getting married next year?\", \"a\": \"(a)Yes\", \"b\": \"(b)No\"}," +
                "{\"question\": \"3.How long have you been married?\", \"a\": \"(a)Less than a year\", \"b\": \"(b)More than a year\"}," +
                "{\"question\": \"4.Have you celebrated your one year anniversary?\", \"a\": \"(a)Yes\" , \"b\": \"(b)No\"}]";

        JSONArray jsonArray = JSON.parseArray(userJson);
        System.out.println(jsonArray);
        List<Question> javaList = jsonArray.toJavaList(Question.class);
        writeToFile(javaList);
    } catch (Exception e) {
        e.printStackTrace();
    }


}

private void writeToFile(List<Question> data) throws Exception {
    BufferedWriter bw;
    try {
        bw = new BufferedWriter(new FileWriter("D:\\test.txt"));
        for (int i = 0; i < data.size(); i++) {
            bw.write(JSON.toJSONString(data.get(i)));
            bw.newLine();
            bw.flush();
        }
        bw.close();
    } catch (Exception e) {
        throw e;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文