简体   繁体   中英

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


}

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

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

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"
    }
]}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM