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

时间:2018-10-03 12:45:15

标签: android json android-volley last.fm jsonexception

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");



                        }

2 个答案:

答案 0 :(得分:1)

您需要使用 JSONObject 作为响应,而不是 JSONArray ,因为您会收到 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");



                    }

答案 1 :(得分:0)

尝试一下...。我已经编辑了您的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");



}