Reputation: 53
I have written a web-service that should return a List of Questions The web-service is working fine and I am getting the response in JSON.
The JSON response is as follows:
{
"QuestionList": [{
"answer": {
"options": ["Andriod is a phone", "Android is a language", "Android is a new fast food", "Android is Mobile App Development Tool"]
},
"id": "0",
"question": "What is Android?"
}, {
"answer": {
"options": ["No", "Yes"]
},
"id": "1",
"question": "Do you know Android?"
}, {
"answer": {
"options": ["No", "Yes", "Yes", "Yes"]
},
"id": "2",
"question": "Do you know Android?"
}, {
"answer": {
"options": ["Interface is contract for a class", "It is class", "It is an abstract class", "none of the above"]
},
"id": "3",
"question": "What is Interface?"
}, {
"answer": {
"options": ["Sonia Gandhi", "Rahul Gandhi", "Manmohan Singh", "Pratibha Patil"]
},
"id": "4",
"question": "Who is PM of India?"
}, {
"answer": {
"options": ["Haryana", "Delhi", "Deheradun", "Darjeling"]
},
"id": "5",
"question": "What is capital of India?"
}, {
"answer": {
"options": ["Abstract Class is a pre defined class", "Partial Implementation of a class", "A class that as have atleast 1 abstract method"]
},
"id": "6",
"question": "What is Abstract Class?"
}]
}
Now I have to convert this response into a List
Here is the Question Class
public class Question {
private int id;
private String question;
private Answer answer;
public Question() {
// TODO Auto-generated constructor stub
}
public Question(int id, String question, Answer answer) {
setId(id);
setQuestion(question);
setAnswer(answer);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public Answer getAnswer() {
return answer;
}
public void setAnswer(Answer answer) {
this.answer = answer;
}
@Override
public String toString() {
return "[ Question id: " + getId()
+ " Question: " + getQuestion()
+ " Answer: " + getAnswer() + " ]";
}
}
Here is the Answer Class
public class Answer {
private List<String> options;
public Answer() {
// TODO Auto-generated constructor stub
}
public Answer(List<String> options) {
setOptions(options);
}
public List<String> getOptions() {
return options;
}
public void setOptions(List<String> options) {
this.options = options;
}
@Override
public String toString() {
return "[ Options: " + getOptions() + " ]";
}
}
I am am not able to figure out how to convert the response to List Pls help...
Upvotes: 2
Views: 4100
Reputation: 38168
Use Gson : it will be as simple as
Gson gson = new GsonBuilder().create();
Answer a = gson.fromJson( response, Answer.class);
System.out.println( a );
Gson is a powerfull open source library that allows an easy mapping from POJOs to Json, and the other way around.
Upvotes: 2
Reputation: 4870
Use this Code
Type collectionType = new TypeToken<List<Your_Object>>() {
}.getType();
List<Your_Object> your_Object = gson.fromJson(jsonString, collectionType);
hope this will help you.
Upvotes: 0