【问题标题】:Parse json objects and arrays android解析json对象和数组android
【发布时间】:2013-03-30 14:12:01
【问题描述】:

我有这个 JSON 想要进入我的 Android 应用程序

{

    "json":{


        "albums":[{"id":"1","name":"Best Sumer colections","singer":"Dj Tiresto","genre":"Trance","mix":"yes","thumb":"alb1.png","songs":
                                            [
                                             {"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                                             {"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                                             {"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                                             {"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                                             {"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                                             {"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},


                                                                                                                                 ]
                  },
                  {"id":"2","name":"Best Sumer colections","singer":"Dj Tiresto","genre":"Trance","mix":"yes","thumb":"alb1.png","songs":
                  [
                   {"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},


                   ]
                  },
                  {"id":"3","name":"O viatza","singer":"Maria Bieshu","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
                  [
                   {"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},


                   ]
                  },
                  {"id":"4","name":"De petrecere","singer":"Gerghe tzopa","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
                  [
                   {"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},


                   ]
                  },
                  {"id":"5","name":"De petrecere","singer":"Gerghe tzopa","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
                  [
                   {"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},


                   ]
                  },
                  {"id":"6","name":"De petrecere 2013","singer":"Gerghe tzopa","genre":"Trance","mix":"no","thumb":"alb1.png","songs":
                  [
                   {"song":"Nothins else1","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else2","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else3","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else4","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else5","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},
                   {"song":"Nothins else6","artist":"ionsuruceanu","mp3":"mp3_URL","mp4":"mp4_URL","thumb":"alb1.png"},


                   ]
                  }

        ]


    }


}

但我只从 logcat 获得Reading a NULL string not supported here.。 所以这是我用来解析 json 的代码:

// Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of albums

            albums = json.getJSONArray(TAG_ALBUMS);

            // looping through All albums
            for(int i = 0; i < albums.length(); i++){
                JSONObject c = albums.getJSONObject(i);

                // Storing each json item in variable
                String album_id = c.getString(TAG_ID);
                String album_name = c.getString(TAG_NAME);
                String album_singer = c.getString(TAG_SINGER);
                String album_genre = c.getString(TAG_GENRE);
                String album_thumb = c.getString(TAG_THUMB);

                // songs are again JSON Object
                JSONObject songs = c.getJSONObject(TAG_SONGS);
                String artist = songs.getString(TAG_SONG_ARTIST);
                String mp3 = songs.getString(TAG_SONG_MP3);
                String mp4 = songs.getString(TAG_SONG_MP4);
                String song_thumb = songs.getString(TAG_SONG_THUMB);
                String song_title = songs.getString(TAG_SONG_TITLE);
                Log.v("--", "Albums \n"+" "+album_id+" "+album_name+" "+ album_genre+" "+album_singer+" "+album_thumb);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

那么谁能帮我解决这个问题。

【问题讨论】:

    标签: android json arrays


    【解决方案1】:

    JSONArray songs = c.getJSONArray(TAG_SONGS);

    songs 是一个 JSONObjects 数组,您应该以与解析 albums 相同的方式处理它。

    【讨论】:

    • 你能给我更详细的解决方案吗..谢谢你的回答
    • songs 是一个 JSON 对象数组,其中每个对象都有 mp3、mp4、artist、thumb 等标签。
    • 除了歌曲部分之外,您做得非常正确。所以,为此,看看stackoverflow.com/questions/9529558/…
    • 现在我得到 03-30 16:46:10.662: W/System.err(21641): org.json.JSONException: No value for albums you can help me now?
    • 当然!您能否在您的问题中发布您更新的代码?
    猜你喜欢
    • 2016-11-20
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    相关资源
    最近更新 更多