下面是我的回答。
{"Table":[{"Count":1,"Result":"R"},{"Count":17,"Result":"Total Questions"},{"Count":16,"Result":"W"}]}我必须解析上面的json,并将问题计数显示为对的、错的、未回答的和全部问题。我试过如下。我得到的所有参数为17,请帮助我。
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
// try {
String url1 = "http://smarteach.com/questions/questions.svc/Learner_Qbank_Stats_Today/val1="
+ learnerid
+ "/val2="
+ courseid
+ "/val3="
+ session_id + "";
System.out.println("Stats from URL : " + url1);
ServiceHandler sh = new ServiceHandler();
jsonstring = sh.makeServiceCall(url1, ServiceHandler.GET);
System.out.println("Response: " + jsonstring);
if (jsonstring != null) {
try {
parent = new JSONObject(jsonstring);
contacts = parent.getJSONArray("Table");
for (int i = 0; i < contacts.length(); i++) {
parent = contacts.getJSONObject(i);
totalquestions = parent.getString("Count");
notanswered = parent.getString("Count");
correctanswered = parent.getString("Count");
wronganswered = parent.getString("Count");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(getActivity(), "Please try after some time",
Toast.LENGTH_LONG).show();
}
return url1;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
p.dismiss();
tvbookmarkcount.setText(totalquestions);
tvquesunattempted.setText(notanswered);
tvcorrectanswered.setText(correctanswered);
tvwronganswered.setText(wronganswered);
}发布于 2014-04-19 07:39:13
我已经为你的问题写了解析。请试试这个。
public void parseJson(String jsonString) {
if (jsonString != null && jsonString.length() > 0) {
try {
JSONObject questionsObject = new JSONObject(jsonString);
JSONArray questionsArray = questionsObject
.getJSONArray("Table");
if (questionsArray != null && questionsArray.length() > 0) {
for (int i = 0; i < questionsArray.length(); i++) {
JSONObject innerQuestionObject = (JSONObject) questionsArray
.get(i);
String count = innerQuestionObject.getString("Count");
String result = innerQuestionObject.getString("Result");
if (result.equalsIgnoreCase("R")) {
correctanswered = count;
} else if (result.equalsIgnoreCase("Total Questions")) {
totalquestions = count;
} else {
notanswered = count;
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}发布于 2014-04-19 07:21:48
只需在setText循环中调用i方法,而不是postexecute方法。只要加上这段代码和@Kanaiya的答案。
for (int i = 0; i < contacts.length(); i++) {
JSONObject obj = contacts.getJSONObject(i);
totalquestions = obj.getString("Count");
notanswered = obj .getString("Count");
correctanswered = obj.getString("Count");
wronganswered = obj.getString("Count");
tvbookmarkcount.setText(totalquestions);
tvquesunattempted.setText(notanswered);
tvcorrectanswered.setText(correctanswered);
tvwronganswered.setText(wronganswered);
}希望它能帮到你。:)
发布于 2014-04-19 07:12:33
请使用不同的JSONObject
for (int i = 0; i < contacts.length(); i++) {
JSONObject obj = contacts.getJSONObject(i);
totalquestions = obj.getString("Count");
notanswered = obj .getString("Count");
correctanswered = obj.getString("Count");
wronganswered = obj.getString("Count");
}https://stackoverflow.com/questions/23167089
复制相似问题