2

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;
    }


}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Yaniv
  • 125
  • 2
  • 10
  • 1
    Does this answer your question? [How do I convert a JSON array into a Java List. I'm using svenson](https://stackoverflow.com/questions/10722587/how-do-i-convert-a-json-array-into-a-java-list-im-using-svenson) – golkarm Jan 27 '21 at 13:50
  • Yeah but can you customize an answer to my object? I don't understand how to do that – Yaniv Jan 27 '21 at 13:59
  • This answer may be easier for you https://stackoverflow.com/a/10726030/3257579, if it isn't tell me – golkarm Jan 27 '21 at 14:02
  • What should I write here> ArrayList list = parser.parse(ArrayList.class, json); ? (json value) – Yaniv Jan 27 '21 at 14:37

4 Answers4

1

You can use the below methods to read from a file and convert the JSON array to a List of Questions.

public String readJsonFromFile(String filePath) throws IOException, ParseException {
    String json = Files.readString(Paths.get("file path"));
    return new JSONParser().parse(json).toString();
}


public List<Question> convert(String JsonString) throws JsonMappingException, JsonProcessingException {
        ObjectMapper om = new ObjectMapper();
        CollectionType typeReference =
                TypeFactory.defaultInstance().constructCollectionType(List.class, Question.class);
        List<Question> questions =  om.readValue(JsonString, typeReference);
        return questions;
    }

If your JSON keys and Java class fields are different, use @JsonPropery annotation to map keys to fields

import com.fasterxml.jackson.annotation.JsonProperty;

public class Question {
    

    @JsonProperty("question")
    private String title;
    @JsonProperty("answer 1")
    private String a1;
    @JsonProperty("answer 2")
    private String a2;
    @JsonProperty("answer 3")
    private String a3;
    @JsonProperty("correct answer")
    private String cA;

    //setters & getters
}

Required Jars:
jackson-annotations.jar
jackson-core.jar
jackson-databind.jar
json-simple.jar //used to parse Json file to String

0

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

golkarm
  • 1,021
  • 1
  • 11
  • 22
0

You can create a new Class Questions.java which has a List of Question object and use the following to convert into a list.

public class Questions {

List<Question> questions;

//getter setter }




Questions questions = null;
    try {
        JsonReader reader = new JsonReader(new FileReader(
                "replace with path"));
        questions = new Gson().fromJson(reader, Questions.class);
    } catch (Exception e) {
        e.printStackTrace();
    }

Example json:

{
"questions": [
    {
        "title": "What is 1+2 ?",
        "a1": "1",
        "a2": "2",
        "a3": "3",
        "cA": "3"
    },
    {
        "title": "What is 2*3 ?",
        "a1": "6",
        "a2": "4",
        "a3": "3",
        "cA": "6"
    }
]}
-1

You can try this:

String json = Files.readString(Paths.get("path\of\your\json\file"));

Map<String,List<Example>> result1 = parser.parse(Map.class, json);
List<Question> list = new ArrayList<Value>(result1.values());

1) read your json file and put it in a string variable

2) deserialize your json in a map

3) convert your map to a list of Question