[英]json parsing with multiple json objects
我在下面有一個json響應。
{"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);
}
只需在i
循環內調用setText方法即可,而不是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);
}
希望對您有幫助。 :)
請使用其他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");
}
我已經為您的問題編寫了解析算法。 請嘗試一下。
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.