我如何才能显示以下json的内容

时间:2014-03-06 11:42:40

标签: java android json

我是Android应用程序开发的新手。我正在开发一个Android应用程序,我需要在下一个按钮的帮助下一个接一个地向用户显示一个问题列表。当用户单击下一个按钮时,下一个问题将与其可供选择的选项列表一起显示。我们会使用MySQLPHP数据库中检索问题和选项,并且响应采用下面显示的JSON格式

{
"questions": [
    {
        "q_id": "18",
        "qtext": "The largest liability appearing in the book of a commercial bank is",
        "option_1": "cash",
        "option_2": "deposits",
        "option_3": "loans and advances",
        "option_4": "capital and reserves",
        "option_5": "treasury bills"
    },
    {
        "q_id": "7",
        "qtext": "A commercial bank is unique in that it is only the institution that",
        "option_1": "makes loans to private people and businessmen",
        "option_2": "accept deposits",
        "option_3": "can store peoples' valuables",
        "option_4": "can transfer money from one place to another for its customers",
        "option_5": "saves money through the granting of credits"
    },
    {
        "q_id": "3",
        "qtext": "When elasticity is zero, the demand curve is",
        "option_1": "Perfectly elastic",
        "option_2": "Perfectly inelastic",
        "option_3": "Concave",
        "option_4": "Downward slopping",
        "option_5": "Circular"
    },
    {
        "q_id": "5",
        "qtext": "Inferior goods are referred to in Economics as goods",
        "option_1": "Whose quality is low",
        "option_2": "Consumed by very poor people",
        "option_3": "Whose consumption falls when consumers' income rises",
        "option_4": "Which satisfy only the basic needs",
        "option_5": "None of the above"
    },
    {
        "q_id": "4",
        "qtext": "The following is NOT a reason for the existence of small firms",
        "option_1": "Scale of production is limited by size of the market",
        "option_2": "Expansion brings diminishing returns",
        "option_3": "Large firms can carter for wide markets",
        "option_4": "Small firms can provide personal services",
        "option_5": "All of the above"
    },
    {
        "q_id": "10",
        "qtext": "If a company doubles all its inputs and discovers that its output more than doubles, we can say that the company is experiencing",
        "option_1": "Increasing Marginal utility",
        "option_2": "Dis-economies of scale",
        "option_3": "Increasing costs",
        "option_4": "Constant returns to scale",
        "option_5": "Increasing returns to scale"
    },
    {
        "q_id": "17",
        "qtext": "Which of the following statements is true?",
        "option_1": "A proportional tax is one which takes from high income people a higher fraction of their income than it takes for low income people",
        "option_2": "taxes on commodities of services which can be shifted elsewhere are usually called direct taxes",
        "option_3": "The sole proprietor is a legal entity",
        "option_4": "the influence of demand on price will be smallest on the short run",
        "option_5": "the cost of production is the most important determining factor of supply in the long run"
    },
    {
        "q_id": "1",
        "qtext": "Suppose that the equilibrium price of an article is $5.00 but the government fixes the price by law at $4.00, the supply will be",
        "option_1": "The same as equilibrium supply",
        "option_2": "Greater than equilibrium supply",
        "option_3": "Less than the equilibrium supply",
        "option_4": "Determined later by government",
        "option_5": "None of these"
    }
],
"success": 1
   }

请如何借助下一个按钮一个接一个地显示这些问题及其选项。

我将非常感谢您的帮助,或者您是否可以向我展示任何显示如何完成此操作的教程的链接。 提前致谢

4 个答案:

答案 0 :(得分:1)

我认为您只需要一个TextView用于您的问题,并需要几个CheckBox来获取答案。

我会将您的JSON-Object中的问题和答案存储在带有Questions-Object的ArrayList中(成员:questionID,question,answer1,answer2,..)

然后你需要一个Button,你可以在其中实现一个OnClickListener。每当用户单击该按钮时,您将TextView和CheckBoxes的文本更改为列表中的下一个Question-Object。

希望这能帮到你!

答案 1 :(得分:0)

解析json,问题是一个数组,这个数组的每个元素都包含问题id,问题测试和这个问题选项。

现在这里的选项可能少于或多于4,以option_1,option_2 ...开头,所以在这里你应该应用一个循环来获得如下选项:

String keyHeader = "option_"
int optionNumber = 1;
boolean loopEnds = false;
while(!loopEnds ){

if(jsonObject.has(keyHeader+String.valueof(optionNumber)){

  //read the option and save it like:
jsonObject.getString(keyHeader+String.valueof(optionNumber));
optionNumber++;
}
else loopEnds  = true;
}

答案 2 :(得分:0)

            try {
                JSONObject jsonObj = new JSONObject("Your jsons Response String");

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray("questions");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String Q_id = c.getString("q_id");
                    String Qtest = c.getString("QText");
                    String Qop2 = c.getString("option_1");


                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }

答案 3 :(得分:0)

您可以使用具有此结构的一个类Question

class Question{
    String id;
    String qtext;
    String opt1;
    String opt2;
    String opt3;
    String opt4;
    String opt5;

    public Question (id, ques, opt1, opt2, opt3, opt4, opt5){
        this.id = id;
        this.qtext = ques;
        this.opt1 = opt1;
        this.opt2 = opt2;
        this.opt3 = opt3;
        this.opt4 = opt4;
        this.opt5 = opt5; 
    }
}

将您的回复数据解析为以下类:

    List<Question> queList = new ArrayList<Question>();
    JSONArray jArray = new JSONArray(responseString);
    int n = jArray.length();
    for (int i = 0; i < n; i++) {
        // GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY
        JSONObject jo = jArray.getJSONObject(i);

        // RETRIEVE EACH JSON OBJECT'S FIELDS
        String id = jo.getString("id");
        String qtext = jo.getString("qtext");
        String opt1 = jo.getString("option1");
        String opt2 = jo.getString("option2");
        String opt3 = jo.getString("option3");
        String opt4 = jo.getString("option4");
        String opt5 = jo.getString("option5");

        Question que = new Question(id, qtext, opt1, opt2, opt3, opt4, opt5);
        queList.add(que);
    }

所以你可以在解析后从列表中使用它。希望这会有所帮助。