我很难解析以下 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)
这段代码的主要目的是为我的文本视图加载一组问题和答案,我显然没有实现。