0

I have a json response below.

{"Table":[{"Count":1,"Result":"R"},{"Count":17,"Result":"Total Questions"},{"Count":16,"Result":"W"}]}

I have to parse the above json and display the question count as Right,Wrong,Not Answered and total Questions. I have tried as below. I am getting all the parameters as 17. Pls help me.

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

        }
1
  • use optJSONArray("Table"); to get the array of JSONObjects. Commented Apr 19, 2014 at 7:16

3 Answers 3

2

Just call the setText method within i loop than postexecute method.. Just add this code along with @Kanaiya's answer..

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

Hope it will help you. :)

Sign up to request clarification or add additional context in comments.

1 Comment

1+ for answer the same
1

I have written parsing algo for your problem. Kindly try this.

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

    }

}

Comments

1

please use different 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");
}

1 Comment

No change.. It doesn't changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.