json使用多个json对象进行解析

时间:2014-04-19 07:08:39

标签: android json

我在下面有一个json回复。

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

我必须解析上面的json并将问题计数显示为Right,Wrong,Not Answered和total Questions。我试过如下。我得到的所有参数都是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);

        }

3 个答案:

答案 0 :(得分:2)

只需在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);
}

希望它会对你有所帮助。 :)

答案 1 :(得分:1)

请使用不同的 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");
}

答案 2 :(得分:1)

我已经为您的问题编写了解析算法。请试试这个。

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

    }

}