繁体   English   中英

如何将 JSON 文件转换为列表

[英]How to convert JSON file to List

我正在尝试打开JSON ,其中每个 object 4 个属性都有一个对象数组:问题、答案 1、答案 2、答案 3、正确答案。 我创建了一个名为 Question 的Class 我想创建一个array /问题list ,然后使用它

JSON 位于名为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;
    }


}

你有两个步骤

  1. 从文件中获取 JSON 字符串,使用从资产中读取文件

  2. 从 JSON 字符串中提取项目使用如何将 JSON 数组转换为 Java 列表。 我正在使用斯文森

你可以试试这个:

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)读取您的 json 文件并将其放入字符串变量中

2)在 map 中反序列化您的 json

3)将您的 map 转换为问题列表

您可以创建一个新的 Class Questions.java ,其中包含一个问题列表 object 并使用以下内容转换为列表。

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

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

您可以使用以下方法从文件中读取并将 JSON 数组转换为问题列表。

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

如果您的 JSON 密钥和 Java class 字段不同,请使用 @JsonPropery 注释到 Z1D7AEZB8DC8ED5122449015 字段

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
}

所需的 Jars:
杰克逊注释.jar
杰克逊核心.jar
杰克逊-databind.jar
json-simple.jar //用于解析Json文件为String

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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