[英]How I can display the contents of the following json
我真的是android應用程序開發的新手。 我正在開發一個Android應用程序,其中我需要在下一個按鈕的幫助下向用戶一個接一個地顯示問題列表。 當用戶單擊“下一步”按鈕時,將出現下一個問題及其選項列表。 使用PHP
從MySQL
數據庫中檢索問題和選項,響應為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
}
請在下一個按鈕的幫助下,如何依次顯示這些問題及其選項集。
非常感謝您的幫助,或者如果您可以給我顯示顯示如何完成此操作的教程的任何鏈接,我將不勝感激。 提前致謝
我認為您只需要一個TextView來回答您的問題,並需要幾個CheckBox來回答您的問題。
我會將來自JSON對象的問題和答案存儲在帶有Questions-Object的ArrayList中(成員:questionID,question,answer1,answer2,..)
然后,您需要一個按鈕,在其中實現OnClickListener。 並且,每當用戶單擊按鈕時,您都將TextView和CheckBox的文本更改為列表中的下一個Question-Object。
希望這對您有所幫助!
解析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;
}
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();
}
您可以使用具有以下結構的一個類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);
}
因此,您可以在解析后從列表中使用它。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.