2014-04-04 55 views
0

請幫我解析頁面!我不介意做什麼修復類型org.json.JSONArray的值無法轉換爲JSONObject

PHP代碼:

[{"DATE_REG":"04.04.2014","NAME_WDAY":"\u041f`\u044f\u0442\u043d\u0438\u0446\u044f","NAME_PAIR":"3 \u043f\u0430\u0440\u0430","TIME_PAIR":"11:25-12:45""},{"DATE_REG":"04.04.2014","NAME_WDAY":"\u041f`\u044f\u0442\u043d\u0438\u0446\u044f","NAME_PAIR":"6 \u043f\u0430\u0440\u0430","TIME_PAIR":"16:35-17:55""}] 

我的代碼:

private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      name2 = (TextView)findViewById(R.id.name); 
      para2 = (TextView)findViewById(R.id.para); 
      aud2 = (TextView)findViewById(R.id.aud); 

} 
       @Override 
       protected JSONObject doInBackground(String... args) { 
        JSONParser jParser = new JSONParser(); 
        JSONObject json = jParser.getJSONFromUrl(url); 
        return json; 
       } 
       @Override 
       protected void onPostExecute(JSONObject json) { 
        pDialog.dismiss(); 
        try { 
          // Getting JSON Array 
         for(int i=0; i < jsonarray.length(); i++) { 
          JSONObject c = new JSONObject(); 
          // Storing JSON item in a Variable 
          String NAME_FIO = c.getString(prepod); 
          String ABBR_DISC = c.getString(para); 
          String NAME_AUD = c.getString(audit); 
          //Set JSON Data in TextView 
          name2.setText(NAME_FIO); 
          para2.setText(ABBR_DISC); 
          aud2.setText(NAME_AUD); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

回答

0

你的PHP返回數組,你正試圖從它解析一個JSONObject。將您的AsyncTask和解析器返回類型更改爲JSONArray,並在您的onPostExecute循環中使用它。

0

在您迴應試試這個..

首先你JSON是無效一個是額外的每個數組元素。

您的JSON下方可能會喜歡..

[ 
    { 
     "DATE_REG": "04.04.2014", 
     "NAME_WDAY": "П`ятниця", 
     "NAME_PAIR": "3 пара", 
     "TIME_PAIR": "11:25-12:45" 
    }, 
    { 
     "DATE_REG": "04.04.2014", 
     "NAME_WDAY": "П`ятниця", 
     "NAME_PAIR": "6пара", 
     "TIME_PAIR": "16: 35-17: 55" 
    } 
] 

這是JSONArray不是JSONObject試着像下面那樣得到

private class JSONParse extends AsyncTask<String, String, JSONArray> { 

  @Override 
      protected JSONArray doInBackground(String... args) { 
       JSONParser jParser = new JSONParser(); 
       JSONArray json = jParser.getJSONFromUrl(url); 
       return json; 
      } 

然後

   @Override 
      protected void onPostExecute(JSONArray json) { 
       pDialog.dismiss(); 
       try { 
         // Getting JSON Array 
        for(int i=0; i < json.length(); i++) { 
         JSONObject c = json.getJSONObject(i); 
         // Storing JSON item in a Variable 
         String NAME_FIO = c.getString(prepod); 
         String ABBR_DISC = c.getString(para); 
         String NAME_AUD = c.getString(audit); 
         //Set JSON Data in TextView 
         name2.setText(NAME_FIO); 
         para2.setText(ABBR_DISC); 
         aud2.setText(NAME_AUD); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
相關問題