如何从json响应中读取状态

时间:2014-04-19 12:48:05

标签: android json

我在下面有一个json回复。

{
   "Table":[
      {
         "status":"failed",
         "message":"Questions exausted for the Course"
      }
   ]
}

我必须从回复中读取状态。我试过如下。但我无法阅读。请帮帮我们。

if (result != null
                && !result.toLowerCase()
                        .startsWith("{\"status\":\"invalid") ) {
            mysql.setLastRowCount(lastrowcount);
             System.out.println("PE result in : " + result);
            try {
                JSONObject resultJson = new JSONObject(result);
                JSONArray resultArray = resultJson.getJSONArray("Table");
                String status=resultJson.getString("status");



                System.out.println("Ravi Today "+status);

                for (int i = 0; i < resultArray.length(); i++) {
                    try {
                        JSONObject tempJSONOBJECT = resultArray
                                .getJSONObject(i);
                        bean.setQuestion(tempJSONOBJECT
                                .getString(Constants.TAG_QUESTION));
                        bean.setOption1(tempJSONOBJECT
                                .getString(Constants.TAG_OPT1));
                        bean.setOption2(tempJSONOBJECT
                                .getString(Constants.TAG_OPT2));
                        bean.setOption3(tempJSONOBJECT
                                .getString(Constants.TAG_OPT3));
                        bean.setOption4(tempJSONOBJECT
                                .getString(Constants.TAG_OPT4));
                        bean.setOption5(tempJSONOBJECT
                                .getString(Constants.TAG_OPT5));
                        bean.setOption6(tempJSONOBJECT
                                .getString(Constants.TAG_OPT6));
                        bean.setOption7(tempJSONOBJECT
                                .getString(Constants.TAG_OPT7));
                        bean.setOption8(tempJSONOBJECT
                                .getString(Constants.TAG_OPT8));
                        bean.setRightAnswer(tempJSONOBJECT
                                .getString(Constants.TAG_ANSWER));
                        bean.setMedia(tempJSONOBJECT
                                .getString(Constants.TAG_MEDIA));
                        bean.setNumberOfOptions(tempJSONOBJECT
                                .getInt(Constants.TAG_NO_OPTIONS));
                        bean.setOptionMode(tempJSONOBJECT
                                .getString(Constants.TAG_MULTI_SELECT));
                        bean.setExplanation(tempJSONOBJECT
                                .getString(Constants.TAG_EXPLANATION));
                        bean.setQuestionID(tempJSONOBJECT
                                .getString(Constants.TAG_QID));
                        bean.setStatus(tempJSONOBJECT
                                .getString(Constants.TAG_STATUS));
                        bean.setMessage(tempJSONOBJECT
                                .getString(Constants.TAG_MESSAGE));
                        bean.setSubjectID(subjectid);
                        mysql.addToQuestionsTable(bean);
                    } catch (Exception _ex) {
                        _ex.printStackTrace();
                    }
                    if (tablehasrows == false) {
                        if (mysql.getTableRowCount("questionmastermain",
                                "SUBJECT_ID", subjectid) > 0) {
                            mysql.getNextQuestion(subjectid, false);
                            tablehasrows = true;
                        }
                    }
                }

2 个答案:

答案 0 :(得分:4)

尝试如下:

JSONObject resultJson = new JSONObject(result);
JSONArray resultArray = resultJson.getJSONArray("Table");

for (int i = 0; i < resultArray.length(); i++) {

   JSONObject b = venues.getJSONObject(i);
   String status = b.getString("status");

}

答案 1 :(得分:1)

试试这个,,,

if(result!=null){
        try {
                JSONObject jsonObj =new JSONObject(result);
                tablejsonarray=jsonObj.getJSONArray("Table");
                for(int i=0;i<tablejsonarray.length();i++){
                    JSONObject l=tablejsonarray.getJSONObject(i);
                    status=l.getInt("status");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
    }