循环和其他方面的JSON解析错误

时间:2013-04-25 13:20:14

标签: android json

我很难解析以下JSON输出:

{
   "status":"success",
   "data":[
      {
         "question_id":"1",
         "category_id":"1",
         "question":"jjj",
         "answer":"ffdf",
         "created_on":"16 Apr, 2013 06:52",
         "modified_on":"",
         "user_id":"1",
         "category_name":"Career",
         "is_answered":true
      },
      {
         "question_id":"3",
         "category_id":"1",
         "question":"ssssssssssss",
         "answer":null,
         "created_on":"23 Apr, 2013 15:12",
         "modified_on":"",
         "user_id":"1",
         "category_name":"Career",
         "is_answered":false
      }
   ],
   "message":null
}

我尝试的代码是:

JSONObject getQuestion;
public class GetQuestionAnswers extends AsyncTask<Void, Void, Void> {
    ProgressDialog progress;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress = new ProgressDialog(QuestionStack.this);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setMessage("Updating Information ...");
        progress.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        UserFunctions users = new UserFunctions();
        getQuestion = users.getAllQuestion(question_user_id);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        try {
            JSONArray data = getQuestion.getJSONArray("data");
            Log.i("DATA", data.toString());
            for (int i = 0; i < data.length(); i++) {
                JSONObject goAccess = data.getJSONObject(i);
                question_JSON = goAccess.getString("question");
                answer_JSON = goAccess.getString("answer");
                Log.i("QUESTION", question_JSON);
                Log.i("ANSWER", answer_JSON);
                question_answer_view.setText(question_JSON + answer_JSON);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        progress.dismiss();
        super.onPostExecute(result);
    }
}

它显示的错误:

04-25 18:54:26.234: W/System.err(2945):     at org.json.JSON.typeMismatch(JSON.java:100)
04-25 18:54:26.234: W/System.err(2945):     at org.json.JSONObject.getJSONArray(JSONObject.java:548)
04-25 18:54:26.244: W/System.err(2945):     at com.example.btshome.QuestionStack$GetQuestionAnswers.onPostExecute(QuestionStack.java:80)
04-25 18:54:26.244: W/System.err(2945):     at com.example.btshome.QuestionStack$GetQuestionAnswers.onPostExecute(QuestionStack.java:1)
04-25 18:54:26.244: W/System.err(2945):     at android.os.AsyncTask.finish(AsyncTask.java:631)
04-25 18:54:26.244: W/System.err(2945):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-25 18:54:26.264: W/System.err(2945):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-25 18:54:26.304: W/System.err(2945):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 18:54:26.304: W/System.err(2945):     at android.os.Looper.loop(Looper.java:137)
04-25 18:54:26.304: W/System.err(2945):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-25 18:54:26.304: W/System.err(2945):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 18:54:26.314: W/System.err(2945):     at java.lang.reflect.Method.invoke(Method.java:511)
04-25 18:54:26.314: W/System.err(2945):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-25 18:54:26.334: W/System.err(2945):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-25 18:54:26.334: W/System.err(2945):     at dalvik.system.NativeStart.main(Native Method)

此代码的主要目的是为我的textview加载一组问题和答案,我显然没有实现。

2 个答案:

答案 0 :(得分:1)

Try this...

    JSONObject js = new JSONObject(jsonString);
    for (int i = 0; i < js.length(); i++) {
        JSONArray data = js.getJSONArray("data");
        for (int j = 0; j < data.length(); j++) {
        JSONObject goAccess = data.getJSONObject(j);
        String question_JSON = goAccess.getString("question");
        String answer_JSON = goAccess.getString("answer");
            if(question_JSON ==null)
                question_JSON = "";
            if(answer_JSON==null)
                answer_JSON  = "";
        Log.i("QUESTION", question_JSON);
        Log.i("ANSWER", answer_JSON);
        question_answer_view.setText(question_JSON + answer_JSON);
        }

    }

答案 1 :(得分:1)

也许你可以尝试这个(如果getQuestion是包含JSON Obejct的结果字符串),在循环之后返回一个字符串中的所有问题/答案:

JSONObject getQuestion;
public class GetQuestionAnswers extends AsyncTask<Void, Void, String> {
    ProgressDialog progress;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress = new ProgressDialog(QuestionStack.this);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setMessage("Updating Information ...");
        progress.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        UserFunctions users = new UserFunctions();
        getQuestion = users.getAllQuestion(question_user_id);
        return getQuestion;
    }

    @Override
    protected void onPostExecute(String result) {
        JSONObject resultObject = new JSONObject(result); 

        try {
            JSONArray data = resultObject.getJSONArray("data");
            Log.i("DATA", data.toString());
            String result = "";
            for (int i = 0; i < data.length(); i++) {
                JSONObject goAccess = data.getJSONObject(i);
                question_JSON = goAccess.getString("question");
                answer_JSON = goAccess.getString("answer");
                Log.i("QUESTION", question_JSON);
                Log.i("ANSWER", answer_JSON);
                result += question_JSON + answer_JSON;
            }
            question_answer_view.setText(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        progress.dismiss();
        super.onPostExecute(result);
    }
}