Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

java - How to convert JSON file to List

I am trying to turn a JSON where there is an array of objects for each object 4 properties: question, answer 1, answer 2, answer 3, correct answer. I created a Class called Question. I want to create an array / list of Question and then use it

The JSON is located in the assets folder named: questions.json

    public class Question{
    private String title;
    private String a1, a2, a3;
    private String cA;

    public Question(String title, String a1, String a2, String a3, String cA){
        this.title = title;
        this.a1 = a1;
        this.a2 = a2;
        this.a3 = a3;
        this.cA = cA;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getA1() {
        return a1;
    }

    public void setA1(String a1) {
        this.a1 = a1;
    }

    public String getA2() {
        return a2;
    }

    public void setA2(String a2) {
        this.a2 = a2;
    }

    public String getA3() {
        return a3;
    }

    public void setA3(String a3) {
        this.a3 = a3;
    }

    public String getcA() {
        return cA;
    }

    public void setcA(String cA) {
        this.cA = cA;
    }


}
question from:https://stackoverflow.com/questions/65920382/how-to-convert-json-file-to-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have two steps

  1. Get JSON string from the file, use read file from assets

  2. Extract items from the JSON string use How do I convert a JSON array into a Java List. I'm using svenson


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...