android无法解析单个json元素

时间:2013-09-13 08:24:30

标签: android json parsing

您好我无法从PHP的Json输出中解析单个json元素。 我的Json输出看起来像这样。

 {"success":1,"question":[{"question":"Question2"}]}

我的代码是

try {
    // Checking for SUCCESS TAG
    int success = json.getInt(TAG_SUCCESS);

    if (success == 1) 
    {
        Questions = json.getJSONArray(TAG_Questions);
        Log.d("users_list.length() : ", String.valueOf( Questions.length())); 

        for (int i = 0; i <  Questions.length(); i++) 
        {
            JSONObject c = Questions.getJSONObject(i);          
            Quensstring[i] =  c.getString("question" + i);
            System.out.println("Exception : " + Quensstring[i]);
        } 
    } else {
        // no products found
        // Launch Add New product Activity
        Intent i = new Intent(getApplicationContext(), day1q1.class);
        // Closing all previous activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

return null;


protected void onPostExecute(String file_url) 
{   
    dialog.dismiss();
    runOnUiThread(new Runnable() 
    {
        public void run() 
        {   
            System.out.println("usersMap.get(User1) : " + Quensstring[0]);  
            Question.setText(Quensstring[0]);   
        }
    });

请帮帮我,我找不到错误。 这是我的logcat

09-13 05:46:08.459: D/All :(1353): {"success":1,"question":[{"question":"Question2"}]}
09-13 05:46:08.459: D/users_list.length() :(1353): 1
09-13 05:46:08.499: W/System.err(1353): org.json.JSONException: No value for question0
09-13 05:46:08.499: W/System.err(1353):     at org.json.JSONObject.get(JSONObject.java:354)
09-13 05:46:08.539: W/System.err(1353):     at org.json.JSONObject.getString(JSONObject.java:510)
09-13 05:46:08.589: W/System.err(1353):     at com.example.newairways.day1q1$Question.doInBackground(day1q1.java:474)
09-13 05:46:08.589: W/System.err(1353):     at com.example.newairways.day1q1$Question.doInBackground(day1q1.java:1)
09-13 05:46:08.589: W/System.err(1353):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-13 05:46:08.589: W/System.err(1353):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-13 05:46:08.589: W/System.err(1353):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-13 05:46:08.589: W/System.err(1353):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-13 05:46:08.599: W/System.err(1353):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-13 05:46:08.599: W/System.err(1353):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-13 05:46:08.626: W/System.err(1353):     at java.lang.Thread.run(Thread.java:856)
09-13 05:46:08.649: I/System.out(1353): usersMap.get(User1) : null

2 个答案:

答案 0 :(得分:2)

{ “成功”:1, “问题”:[<强> { “问题”: “问题2”} ]}。您的JSONArray只有一个JSONObject,而JSONObject只有一个标记“question”。所以替换你的代码

Quensstring[i] =  c.getString("question" + i);

String quensString =  c.getString("question");

希望这能解决问题。

答案 1 :(得分:1)

替换以下行:

 Quensstring[i] =  c.getString("question" + i);    

使用:

 Quensstring[i] =  c.getString("question");    

由于json字符串标记仅为question