在 android 中使用 last.fm API 时出现 JSONException

JSONException while using last.fm API in android

JSON :

{"results":{"opensearch:Query":{"#text":"","role":"request","searchTerms":"Rose","startPage":"1"},"opensearch:totalResults":"102325","opensearch:startIndex":"0","opensearch:itemsPerPage":"1","artistmatches":{"artist":[{"name":"Guns N' Roses","listeners":"3198315","mbid":"eeb1195b-f213-4ce1-b28c-8565211f8e43","url":"https://www.last.fm/music/Guns+N%27+Roses","streamable":"0","image":[{"#text":"https://lastfm-img2.akamaized.net/i/u/34s/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"small"},{"#text":"https://lastfm-img2.akamaized.net/i/u/64s/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"medium"},{"#text":"https://lastfm-img2.akamaized.net/i/u/174s/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"large"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"extralarge"},{"#text":"https://lastfm-img2.akamaized.net/i/u/300x300/7d102ebcf4184bb1ae2b851efcbceb30.png","size":"mega"}]}]},"@attr":{"for":"Rose"}}}

我无法使用此 JSON 获取艺术家详细信息。 我想知道在 Volley android 中使用此 JSON 的格式。

代码:

final JsonArrayRequest jsonArrayRequest = new 

JsonArrayRequest(Request.Method.GET,url,null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {

            for (int i = 0; i < response.length(); i++) {
                try {

                   JSONObject object = response.getJSONObject(i);

                        JSONObject obj1   = object.getJSONObject("results");
                        JSONObject obj2   = obj1.getJSONObject("artistmatches");
                        JSONArray m_jArry = obj2.getJSONArray("artist");


                        for (i = 0; i < m_jArry.length(); i++)
                        {
                            JSONObject obj4 = m_jArry.getJSONObject(i);
                            String name=obj4.getString("name");
                            String mbid=obj4.getString("mbid");
                            String url=obj4.getString("url");



                        }

您需要使用 JSONObject 作为 Response 而不是 JSONArray 因为,您从 API.

收到 JSONObject

像下面这样使用:

JsonArrayRequest(Request.Method.GET,url,null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {

        //for (int i = 0; i < response.length(); i++) { Removed for loop
            try {

               JSONObject object = response; //We're using JSONObject from API here

                    JSONObject obj1   = object.getJSONObject("results");
                    JSONObject obj2   = obj1.getJSONObject("artistmatches");
                    JSONArray m_jArry = obj2.getJSONArray("artist");


                    for (i = 0; i < m_jArry.length(); i++)
                    {
                        JSONObject obj4 = m_jArry.getJSONObject(i);
                        String name=obj4.getString("name");
                        String mbid=obj4.getString("mbid");
                        String url=obj4.getString("url");



                    }

试试这个....我已经编辑了你的 jsonResponse

    JSONObject jsonObject = new JSONObject(response);
JSONObject results = jsonObject.getJSONObject("results");
JSONObject opensearch = results.getJSONObject("opensearch:Query");
String text = opensearch.getString("#text");//after this get your all string of "opensearch:Query" object same way



String totalResults=results.getString("opensearch:totalResults");////after this get your all other string like "opensearch:startIndex" and "opensearch:itemsPerPage" of "results" object same way 

JSONObject artistmatches = results.getJSONObject("artistmatches");
JSONArray artist = artistmatches.getJSONArray("artist");
JSONArray image = artist.getJSONArray("image");
for (int i = 0; i < artist.length(); i++) {
JSONObject allartist = artist.getJSONObject(i);

String name=artist.getString("name"); // same you can get all strings like "listeners","mbid".. of artist array

//by this you will get images array

for (int j = 0; j < image.length(); j++) {
JSONObject allimage = image.getJSONObject(i);

String imageText=allimage.getString("#text");
String size=allimage.getString("size");



}