0

次の 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)

このコードの主な目的は、一連の質問と回答をテキストビューにロードすることですが、これは明らかに達成していません。

4

2 に答える 2

1

おそらく、これを試すことができgetQuestionます (結果の文字列に JSON オブジェクトが含まれている場合)。ループの後にすべての質問/回答が 1 つの文字列に返されます。

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);
    }
}
于 2013-04-25T13:34:31.730 に答える
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);
        }

    }
于 2013-04-25T13:30:33.403 に答える