简体   繁体   English

我如何显示以下json的内容

[英]How I can display the contents of the following json

I am really new in android app development. 我真的是android应用程序开发的新手。 I am developing an android app in which I need to display a list of questions one after the other to the user with the help of a next button. 我正在开发一个Android应用程序,其中我需要在下一个按钮的帮助下向用户一个接一个地显示问题列表。 When the user clicks next button, the next question appears together with its list of options to choose from. 当用户单击“下一步”按钮时,将出现下一个问题及其选项列表。 The questions and options are retrieved from a MySQL database with PHP and the response is in JSON format shown below 使用PHPMySQL数据库中检索问题和选项,响应为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
   }

Please how can I display these questions and their set of options one after the other with the help of a next button. 请在下一个按钮的帮助下,如何依次显示这些问题及其选项集。

I will really appreciate your help or if you can show me any link showing a tutorial on how to accomplish this. 非常感谢您的帮助,或者如果您可以给我显示显示如何完成此操作的教程的任何链接,我将不胜感激。 Thanks in advance 提前致谢

I think you only need a TextView for your question and several CheckBoxes for your answers. 我认为您只需要一个TextView来回答您的问题,并需要几个CheckBox来回答您的问题。

I would store the questions and answers from your JSON-Object in an ArrayList with a Questions-Object (members: questionID, question, answer1, answer2,..) 我会将来自JSON对象的问题和答案存储在带有Questions-Object的ArrayList中(成员:questionID,question,answer1,answer2,..)

Then you need a Button, where you implement an OnClickListener. 然后,您需要一个按钮,在其中实现OnClickListener。 And whenever the user clicks the button you change the text of the TextView and the CheckBoxes to the next Question-Object in your list. 并且,每当用户单击按钮时,您都将TextView和CheckBox的文本更改为列表中的下一个Question-Object。

Hope this helps you! 希望这对您有所帮助!

Parse the json, questions is an array and each element of this array consists question id, ques test and this question options. 解析json,问题是一个数组,该数组的每个元素都包含问题ID,问题测试和此问题选项。

Now here options maybe less or more than 4 which starts as option_1, option_2 ..., so here you should have to apply a loop to get the options like: 现在,这里的选项可能少于或大于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;
}
            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();
            }

You can use one class Question with this structure: 您可以使用具有以下结构的一个类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; 
    }
}

Parse your response data to class as: 将您的响应数据解析为以下类:

    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);
    }

So you can use it from list after parsing. 因此,您可以在解析后从列表中使用它。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM