首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >多个json对象的json解析

多个json对象的json解析
EN

Stack Overflow用户
提问于 2014-04-19 07:08:38
回答 3查看 132关注 0票数 0

下面是我的回答。

代码语言:javascript
复制
{"Table":[{"Count":1,"Result":"R"},{"Count":17,"Result":"Total Questions"},{"Count":16,"Result":"W"}]}

我必须解析上面的json,并将问题计数显示为对的、错的、未回答的和全部问题。我试过如下。我得到的所有参数为17,请帮助我。

代码语言:javascript
复制
@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);

        }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-19 07:39:13

我已经为你的问题写了解析。请试试这个。

代码语言:javascript
复制
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();
        }

    }

}
票数 1
EN

Stack Overflow用户

发布于 2014-04-19 07:21:48

只需在setText循环中调用i方法,而不是postexecute方法。只要加上这段代码和@Kanaiya的答案。

代码语言:javascript
复制
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);
}

希望它能帮到你。:)

票数 2
EN

Stack Overflow用户

发布于 2014-04-19 07:12:33

请使用不同的JSONObject

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23167089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档