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


Try this

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{


    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
       // portrait Arrangement
    }       
    else 
    { 
        // Landscape Arrangement
    }
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationPortrait || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
4

2 に答える 2

2

Gsonを使用してください:それは次のように簡単です

Gson gson = new GsonBuilder().create();
Answer a = gson.fromJson( response, Answer.class);
System.out.println( a );

Gson は、POJO から Json への簡単なマッピング、およびその逆を可能にする強力なオープン ソース ライブラリです。

于 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 に答える