【问题标题】:How to convert JSON file to List如何将 JSON 文件转换为 List
【发布时间】:2021-01-27 13:41:33
【问题描述】:

我正在尝试将 JSON 转换为每个对象 4 个属性的对象数组:问题、答案 1、答案 2、答案 3、正确答案。 我创建了一个名为“问题”的Class。 我想创建一个 array / list 的 Question 然后使用它

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


}

【问题讨论】:

标签: java android json list


【解决方案1】:

你有两个步骤

  1. 从文件中获取JSON字符串,使用read file from assets

  2. 从 JSON 字符串中提取项目使用 How do I convert a JSON array into a Java List. I'm using svenson

【讨论】:

  • 抱歉,我没听懂,请问您可以和我聊天吗?
【解决方案2】:

你可以试试这个:

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) 在地图中反序列化您的 json

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

【讨论】:

  • 抱歉,我没听懂,请问您可以和我聊天吗?
  • 抱歉,我要如何打开与您的聊天?我不知道该怎么做
  • 我在房间里。可以进入(房间名称:Json to list)
  • 在这里给我发一个链接
【解决方案3】:

您可以创建一个新的 Class Questions.java,其中包含一个 List of Question 对象,并使用以下内容转换为一个列表。

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

【讨论】:

    【解决方案4】:

    您可以使用以下方法从文件中读取并将 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 类字段不同,请使用 @JsonPropery 注解将键映射到字段

    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
    }
    

    所需的罐子:
    jackson-annotations.jar
    jackson-core.jar
    jackson-databind.jar
    json-simple.jar //用来解析Json文件到 字符串

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 2014-08-12
      • 2020-09-15
      • 2014-09-09
      • 2019-05-08
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多