Java 如何显示以下json的内容
我对android应用程序开发非常陌生。我正在开发一个android应用程序,在这个程序中,我需要在“下一步”按钮的帮助下,一个接一个地向用户显示问题列表。当用户单击“下一步”按钮时,将显示下一个问题及其可供选择的选项列表。这些问题和选项是通过Java 如何显示以下json的内容,java,android,json,Java,Android,Json,我对android应用程序开发非常陌生。我正在开发一个android应用程序,在这个程序中,我需要在“下一步”按钮的帮助下,一个接一个地向用户显示问题列表。当用户单击“下一步”按钮时,将显示下一个问题及其可供选择的选项列表。这些问题和选项是通过PHP从MySQL数据库中检索的,响应的格式如下所示 { "questions": [ { "q_id": "18", "qtext": "The largest liability appearing in the
PHP
从MySQL
数据库中检索的,响应的格式如下所示
{
"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
}
请告诉我如何在“下一步”按钮的帮助下一个接一个地显示这些问题及其选项集
我将非常感谢你的帮助,或者如果你能给我看任何关于如何实现这一点的教程的链接。
提前感谢解析json,questions是一个数组,该数组的每个元素包括question id、ques test和this question选项 现在,这里的选项可能小于或大于4,以选项1、选项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;
}
我认为你的问题只需要一个文本视图,答案只需要几个复选框 我会将JSON对象中的问题和答案存储在带有问题对象的ArrayList中(成员:questionID、question、answer1、answer2,…) 然后需要一个按钮,在这里实现OnClickListener。每当用户单击按钮时,您都会将TextView的文本和复选框更改为列表中的下一个问题对象 希望这对你有帮助
试试看{
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();
}
JSONObject jsonObj=新的JSONObject(“您的jsons响应字符串”);
//获取JSON数组节点
contacts=jsonObj.getJSONArray(“问题”);
//通过所有触点循环
对于(int i=0;i
您可以将一个类问题
用于此结构:
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);
}
List queList=new ArrayList();
JSONArray jArray=新JSONArray(responseString);
int n=jArray.length();
对于(int i=0;i
所以,您可以在解析后从列表中使用它。希望这有帮助。确保json是有效的。这里已经回答了这个问题:谢谢神秘魔法。你解释的第二部分将在我用来一个接一个显示问题的活动课上?在课堂提问中,关于每个变量的getter和setter呢?我需要它们吗?第二部分将是您得到响应的地方(当然,在活动中),您是否需要getter setter将取决于您的需求。我认为您不需要setter,因为在创建对象时已经设置了类的所有变量。如果您的需求是获取特定变量,那么您可以创建getter@wallyPlease现在有一个异常,即org.json.JSONException:org.json.JSONArray类型的值(myjson)无法转换为JSONObject。我该怎么办?我将非常感谢你的帮助@Mystic MagicPlease您能告诉我如何在解析后使用列表中的“if”吗?提前谢谢@MysticMagic@wally解析后如何使用列表中的“if”是什么意思?我不明白。谢谢xandi。这就是我计划在文本视图中显示每个问题时所做的,而选项集将位于由5个单选按钮组成的单选组中。请您展示一下如何实现您的解释的代码好吗?提前谢谢@xandiHallo,我会这样实现它。int指数=0;List List=新建ArrayList();//用JSON对象mButton.setOnclickListener(新视图.OnClickListener(){@Override public void onClick(视图v){mTextfeld.setText(list.get(index).getQuestion);mAnswer1.setText(list.get(index).getAnswer1);mAnswer2.setText(list.get(index).getAnswer2);//对于所有其他选项});