2016-09-30 79 views
0

我想從此數據中獲取數據到android。但它失敗並且出現錯誤org.json.JSONObject無法轉換爲JSONArray。我不知道錯誤在哪裏。我爲這件事而鬥爭。請幫助。謝謝JSONObject無法轉換爲JSONArray - Android

{ 
    "categories" : { 
    "href" : "https://api.spotify.com/v1/browse/categories?offset=0&limit=20", 
    "items" : [ { 
     "href" : "https://api.spotify.com/v1/browse/categories/toplists", 
     "icons" : [ { 
     "height" : 275, 
     "url" : "https://t.scdn.co/media/derived/toplists_11160599e6a04ac5d6f2757f5511778f_0_0_275_275.jpg", 
     "width" : 275 
     } ], 
     "id" : "toplists", 
     "name" : "Top Lists" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/mood", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/original/mood-274x274_976986a31ac8c49794cbdc7246fd5ad7_274x274.jpg", 
     "width" : 274 
     } ], 
     "id" : "mood", 
     "name" : "Mood" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/party", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/links/partyicon_274x274.jpg", 
     "width" : 274 
     } ], 
     "id" : "party", 
     "name" : "Party" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/pop", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/pop-274x274_447148649685019f5e2a03a39e78ba52_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "pop", 
     "name" : "Pop" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/popculture", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/trending-274x274_7b238f7217985e79d3664f2734347b98_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "popculture", 
     "name" : "Trending" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/focus", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/original/genre-images-square-274x274_5e50d72b846a198fcd2ca9b3aef5f0c8_274x274.jpg", 
     "width" : 274 
     } ], 
     "id" : "focus", 
     "name" : "Focus" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/rock", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/rock_9ce79e0a4ef901bbd10494f5b855d3cc_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "rock", 
     "name" : "Rock" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/indie_alt", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/indie-274x274_add35b2b767ff7f3897262ad86809bdb_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "indie_alt", 
     "name" : "Indie" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/edm_dance", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/edm-274x274_0ef612604200a9c14995432994455a6d_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "edm_dance", 
     "name" : "EDM/Dance" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/chill", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/chill-274x274_4c46374f007813dd10b37e8d8fd35b4b_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "chill", 
     "name" : "Chill" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/dinner", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/original/dinner_1b6506abba0ba52c54e6d695c8571078_274x274.jpg", 
     "width" : 274 
     } ], 
     "id" : "dinner", 
     "name" : "Dinner" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/sleep", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/sleep-274x274_0d4f836af8fab7bf31526968073e671c_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "sleep", 
     "name" : "Sleep" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/hiphop", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/original/hip-274_0a661854d61e29eace5fe63f73495e68_274x274.jpg", 
     "width" : 274 
     } ], 
     "id" : "hiphop", 
     "name" : "Hip Hop" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/workout", 
     "icons" : [ { 
     "height" : null, 
     "url" : "https://t.scdn.co/media/links/workout-274x274.jpg", 
     "width" : null 
     } ], 
     "id" : "workout", 
     "name" : "Workout" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/rnb", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/r-b-274x274_fd56efa72f4f63764b011b68121581d8_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "rnb", 
     "name" : "RnB" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/country", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/icon-274x274_6a35972b380f65dc348e0c798fe626a4_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "country", 
     "name" : "Country" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/folk_americana", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/folk-274x274_ced3f75528ac61faf505863f7d7fae64_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "folk_americana", 
     "name" : "Folk & Americana" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/metal", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/original/metal_27c921443fd0a5ba95b1b2c2ae654b2b_274x274.jpg", 
     "width" : 274 
     } ], 
     "id" : "metal", 
     "name" : "Metal" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/soul", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/soul-274x274_266bc900b35dda8956380cffc73a4d8c_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "soul", 
     "name" : "Soul" 
    }, { 
     "href" : "https://api.spotify.com/v1/browse/categories/travel", 
     "icons" : [ { 
     "height" : 274, 
     "url" : "https://t.scdn.co/media/derived/travel-274x274_1e89cd5b42cf8bd2ff8fc4fb26f2e955_0_0_274_274.jpg", 
     "width" : 274 
     } ], 
     "id" : "travel", 
     "name" : "Travel" 
    } ], 
    "limit" : 20, 
    "next" : "https://api.spotify.com/v1/browse/categories?offset=20&limit=20", 
    "offset" : 0, 
    "previous" : null, 
    "total" : 32 
    } 
} 

這是我的java.class

class GetKat extends AsyncTask<String,String,String> { 
    int sukses = 0; 
    int maks=0; 
    int TotalTrack=0; 
    String id; 
    public GetKat() { 
     pDialog = new ProgressDialog(CategoryActivity.this); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog.setMessage("Please Wait..."); 
     pDialog.setCancelable(false); 
     pDialog.setIndeterminate(false); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     gridItem item=null; 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("access_token", Token)); 
     params.add(new BasicNameValuePair("limit", "10")); 
     try { 
      JSONObject json = jParser.makeHttpRequest(BASE_URL, "GET", params); 
      if (json != null) { 

       nameList = new ArrayList<HashMap<String,String>>(); 
       Log.d("Semua Nama: ", json.toString()); 
       names = json.getJSONArray(TAG_HASIL); 
       for(int i = 0; i < maks ;i++) 
       { 
        JSONObject c = names.getJSONObject(i); 
        String id = c.getString("id"); 
        String name=c.getString("name"); 
        String url=c.getJSONObject("icons").getString("url"); 

        HashMap<String,String> map = new HashMap<String,String>(); 

        item=new gridItem(); 
        item.setId(id); 
        item.setImage(url); 
        item.setKategori(name); 

        map.put("id",id); 
        map.put("name",name); 
        map.put("url",url); 
        nameList.add(map); 
        mGridData.add(item); 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     pDialog.dismiss(); 
     CustomGridViewAdapter adapter = new CustomGridViewAdapter(
       CategoryActivity.this,R.layout.grid_item,mGridData); 
     if(mGridData.size()>0) 
     { 
      mGridAdapter.setGridData(mGridData); 
      data.setAdapter(adapter); 
     } 
     else 
      Toast.makeText(getApplicationContext(), "No Data Available", Toast.LENGTH_SHORT).show(); 

    } 
} 

堆棧跟蹤

W/System.err: org.json.JSONException: Value {"href":"https:\/\/api.spotify.com\/v1\/browse\/categories?offset=0&limit=10","items":[{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/toplists","icons":[{"height":275,"url":"https:\/\/t.scdn.co\/media\/derived\/toplists_11160599e6a04ac5d6f2757f5511778f_0_0_275_275.jpg","width":275}],"id":"toplists","name":"Top Lists"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/mood","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/original\/mood-274x274_976986a31ac8c49794cbdc7246fd5ad7_274x274.jpg","width":274}],"id":"mood","name":"Mood"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/party","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/links\/partyicon_274x274.jpg","width":274}],"id":"party","name":"Party"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/pop","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/pop-274x274_447148649685019f5e2a03a39e78ba52_0_0_274_274.jpg","width":274}],"id":"pop","name":"Pop"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/popculture","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/trending-274x274_7b238f7217985e79d3664f2734347b98_0_0_274_274.jpg","width":274}],"id":"popculture","name":"Trending"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/focus","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/original\/genre-images-square-274x274_5e50d72b846a198fcd2ca9b3aef5f0c8_274x274.jpg","width":274}],"id":"focus","name":"Focus"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/rock","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/rock_9ce79e0a4ef901bbd10494f5b855d3cc_0_0_274_274.jpg","width":274}],"id":"rock","name":"Rock"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/indie_alt","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/indie-274x274_add35b2b767ff7f3897262ad86809bdb_0_0_274_274.jpg","width":274}],"id":"indie_alt","name":"Indie"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/edm_dance","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/edm-274x274_0ef612604200a9c14995432994455a6d_0_0_274_274.jpg","width":274}],"id":"edm_dance","name":"EDM\/Dance"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/chill","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/chill-274x274_4c46374f007813dd10b37e8d8fd35b4b_0_0_274_274.jpg","width":274}],"id":"chill","name":"Chill"}],"limit":10,"next":"https:\/\/api.spotify.com\/v1\/browse\/categories?offset=10&limit=10","offset":0,"previous":null,"total":32} at categories of type org.json.JSONObject cannot be converted to JSONArray 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at org.json.JSON.typeMismatch(JSON.java:100) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at org.json.JSONObject.getJSONArray(JSONObject.java:588) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at com.project.plbtw.songknow.CategoryActivity$GetKat.doInBackground(CategoryActivity.java:159) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at com.project.plbtw.songknow.CategoryActivity$GetKat.doInBackground(CategoryActivity.java:128) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:295) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:  at java.lang.Thread.run(Thread.java:818) 
+0

使用此URL查看你的json如何:http://www.jsoneditoronline.org/ – mfruizs2

+0

什麼是TAG_HASIL的字符串值? – Cool7

+0

類別@Prashant –

回答

1

你在你的代碼有問題的行:

String url=c.getJSONObject("icons").getString("url"); 

解決方法: 1)使用 「項目」 來代替 「TAG_HASIL」 的象下面這樣:

names = json.getJSONArray("items"); 

第二:地方

String url=c.getJSONObject("icons").getString("url"); 

用途:

JSONObject job=c.getJSONArray("icons").get[0].getString("url"); 
String url=job.getString("url"); 
+1

get [0] getString(「url」); –

+0

正確,你可以使用get [0] .getString(「url」); –

0

請使用net.sf.json庫,你可以從這裏得到: http://java2s.com/Code/Jar/j/Downloadjsonlibjar.htm

String aa =json.toString(); // json is jsonObject 
JSONArray jarray = (JSONArray) JSONSerializer.toJSON(aa); 
ray

jarray現在是一個json數組。

如果您得到任何classNotFound錯誤只需下載支持jar。

1

類別是JSON Object。其中itemsJSON Array。你是不是考慮categories,而是直接試圖重複items


使用gson更好的靈活性。您不需要此手動轉換。 以下內容添加到您的依賴

dependencies { 
    compile 'com.solidfire.code.gson:gson:2.6.2' 
} 

使用GSON格式插件Look at this video解釋如何實現這個插件

現在相應重新格式化您的代碼。請通過this helpful link來解釋這種情況並簡化編碼。

相關問題