2

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...


Alternative for Adobe Dreamweaver with drag and drop design

Is there any such a great html editor with drag and drop design interface which is equivalent to Adobe Dreamweaver.

I googled so many such as Komodo, Aptana, Coffee Cup.Of course, those are good html editors but they do not have design interface where we can drag and drop html elements and align them(What we can do by Dreamweaver).

Can you please suggest me if there is any such great tool for Windows OS.

Thanks in advance.....

Note: This question may be duplicate one.But I have seen already some of the relevant questions in StackOverflow but no question has been answered as what I want actually.So that I posted again..So please dont mark it as duplicate

4

2 回答 2

2

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.

于 2012-04-27T05:42:28.097 回答
0

使用此代码

 Type collectionType = new TypeToken<List<Your_Object>>() {
            }.getType();

 List<Your_Object> your_Object = gson.fromJson(jsonString, collectionType);

希望这会帮助你。

于 2012-04-27T05:45:30.873 回答