通过GSON从JSON列表中的多个对象获取多个属性

时间:2016-10-20 08:26:29

标签: java json gson

我有一个包含3个对象的json:问题,标签和选择。

[
    {
        "question" : {
            "questionId" : 01,
            "isActive" : 1
        },
        "tags" : ["tag1", "tag2"],
        "choices" : [{
                "choiceId" : 0,
                "questionId" : 0
            }, {
                "choiceId" : 1,
                "questionId" : 0
            }
        ]
    }
]

我目前正在使用GSON和这个Extractor类,目的是从3个不同的对象中获取详细信息:

public class Extractor {

//question, tags, and choices are existing classes
Questions question;
List<Tags> tags;
List<Choice> choices;

//getters and setters
}

Extractor result = gson.fromJson(jsonString, Extractor.class);

//I want to see the question, tag, and choice parts from the json
System.out.println("Result" + result.getTags().get(0).getName() + result.getChoices().get(0).getChoiceId());

我做错了什么?如何单独获取问题,标签和选项的属性?另外,我如何能够遍历包含多个问题,标签和选择的JSONlist?

1 个答案:

答案 0 :(得分:0)

只要正确定义了POJO类,下面的代码就应该分别得到问题,标签和选择。请参阅类似post的POJO类定义。

public static void main(String[] args) {

        String jsonString = "[{\"question\":{\"questionId\":01,\"isActive\":1},\"tags\":[\"tag1\",\"tag2\"],\"choices\":[{\"choiceId\":0,\"questionId\":0},{\"choiceId\":1,\"questionId\":0}]}]";
        Gson gson = new Gson();

        JsonParser parser = new JsonParser();
        JsonArray array = parser.parse(jsonString).getAsJsonArray();

        for (final JsonElement json : array) {
            Extracter jsonModel = gson.fromJson(json, new TypeToken<Extracter>() {
            }.getType());

            System.out.println(jsonModel.toString());

            List<Choices> choicesList = jsonModel.getChoices();
            //Looping the choices
            for (Choices choice :choicesList) {
                System.out.println("Choice :"  + choice);
                System.out.println("Choice Id :"  + choice.getChoiceId());
                System.out.println("Question Id :"  + choice.getQuestionId());
            }

            //Printing the question
            System.out.println("Question obj data :" + jsonModel.getQuestion());
            System.out.println("Course Id :" + jsonModel.getQuestion().getQuestionId());
            System.out.println("Active :" + jsonModel.getQuestion().getIsActive());

            //Printing the tags
            System.out.println(jsonModel.getTags());

        }

    }

<强>输出: -

Choice :Choices [choiceId=0, questionId=0]
Choice Id :0
Question Id :0
Choice :Choices [choiceId=1, questionId=0]
Choice Id :1
Question Id :0
Question obj data :Question [questionId=1, courseId=null, isActive=1]
Course Id :1
Active :1
[tag1, tag2]

POJO课程: -

public class Extracter implements Serializable {

    private static final long serialVersionUID = -2255013835370141266L;
    private List<Choices> choices;
    private List<String> tags;
    private Question question;

    public List<Choices> getChoices() {
        return choices;
    }
    public void setChoices(List<Choices> choices) {
        this.choices = choices;
    }
    public List<String> getTags() {
        return tags;
    }
    public void setTags(List<String> tags) {
        this.tags = tags;
    }
    public Question getQuestion() {
        return question;
    }
    public void setQuestion(Question question) {
        this.question = question;
    }
    @Override
    public String toString() {
        return "JsonModel [choices=" + choices + ", tags=" + tags + ", question=" + question + "]";
    }

}


public class Choices implements Serializable{

    private static final long serialVersionUID = 3947337014862847527L;

    private Integer choiceId;
    private Integer questionId;

    public Integer getChoiceId() {
        return choiceId;
    }
    public void setChoiceId(Integer choiceId) {
        this.choiceId = choiceId;
    }
    public Integer getQuestionId() {
        return questionId;
    }
    public void setQuestionId(Integer questionId) {
        this.questionId = questionId;
    }
    @Override
    public String toString() {
        return "Choices [choiceId=" + choiceId + ", questionId=" + questionId + "]";
    }



}


public class Question implements Serializable{

    private static final long serialVersionUID = -8649775972572186614L;

    private Integer questionId;
    private Integer courseId;
    private Integer isActive;

    public Integer getQuestionId() {
        return questionId;
    }
    public void setQuestionId(Integer questionId) {
        this.questionId = questionId;
    }
    public Integer getCourseId() {
        return courseId;
    }
    public void setCourseId(Integer courseId) {
        this.courseId = courseId;
    }
    public Integer getIsActive() {
        return isActive;
    }
    public void setIsActive(Integer isActive) {
        this.isActive = isActive;
    }
    @Override
    public String toString() {
        return "Question [questionId=" + questionId + ", courseId=" + courseId + ", isActive=" + isActive + "]";
    }


}