首先,您的原始响应是不正确的。如果您将回复粘贴到http://jsonlint.com/
您将在第 12 行收到解析错误。即使 jsonschema2pojo 也在您的原始响应中显示错误。首先修改它喜欢
{
"paper": [
{
"que": {
"qid": "1",
"question": "????????????????"
},
"ans": [
{
"id": "1",
"answer": "uiuyiyityu"
},
{
"id": "2",
"answer": "ytuyutyuty"
},
{
"id": "3",
"answer": "retre retret"
},
{
"id": "4",
"answer": "rtretret"
}
]
}
]
}
然后在 jsonschema2pojo 工具上制作 POJO。该工具将创建四个不同的 POJO,如下所示:
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class An {
@SerializedName("id")
@Expose
private String id;
@SerializedName("answer")
@Expose
private String answer;
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The answer
*/
public String getAnswer() {
return answer;
}
/**
*
* @param answer
* The answer
*/
public void setAnswer(String answer) {
this.answer = answer;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Paper {
@SerializedName("paper")
@Expose
private List<Paper_> paper = new ArrayList<Paper_>();
/**
*
* @return
* The paper
*/
public List<Paper_> getPaper() {
return paper;
}
/**
*
* @param paper
* The paper
*/
public void setPaper(List<Paper_> paper) {
this.paper = paper;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Paper_ {
@SerializedName("que")
@Expose
private Que que;
@SerializedName("ans")
@Expose
private List<An> ans = new ArrayList<An>();
/**
*
* @return
* The que
*/
public Que getQue() {
return que;
}
/**
*
* @param que
* The que
*/
public void setQue(Que que) {
this.que = que;
}
/**
*
* @return
* The ans
*/
public List<An> getAns() {
return ans;
}
/**
*
* @param ans
* The ans
*/
public void setAns(List<An> ans) {
this.ans = ans;
}
}
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Que {
@SerializedName("qid")
@Expose
private String qid;
@SerializedName("question")
@Expose
private String question;
/**
*
* @return
* The qid
*/
public String getQid() {
return qid;
}
/**
*
* @param qid
* The qid
*/
public void setQid(String qid) {
this.qid = qid;
}
/**
*
* @return
* The question
*/
public String getQuestion() {
return question;
}
/**
*
* @param question
* The question
*/
public void setQuestion(String question) {
this.question = question;
}
}
现在,您可以通过调用 POJO 的方法非常轻松地获取您的 qid 和 id。使用此代码块获取 qid 和 id。
Paper paper = new Paper();
List<Paper_> papers = paper.getPaper();
for(Paper_ paper_ : papers){
int qid = paper_.getQue().getQid();
List<An> answers = paper_.getAns();
for(An answer : answers){
int ansId = answer.getId();
}
}
希望它会奏效。