如何从这个.json文件中编写Json Parser类

时间:2013-05-14 11:52:06

标签: android json media-player

我希望我的媒体播放器(android)应该播放来自此网址的歌曲。我正在使用JSON文件每天更新​​我的播放列表文件。 请帮助我,我被困在这 感谢。

{
    "json": {
        "Songs": [
            {
                "song": "Song name ",
                "Artist": "Sonjay singh",
                "mp3": "http://c2lo.reverbnation.com/audio_player/download_song_direct/9140921.mp3"
            },
            {
                "song": "SONG NAME",
                "Artist": "aritist",
                "mp3": "mp3_URL"
            },
            {
                "song": "SONG NAME",
                "Artist": "aritist",
                "mp3": "mp3_URL"
            }
        ]
    }
}

我上了这堂课,但仍然发现如何使用它...感谢帮助我。

public class Json{
private List songs;

public List getSongs(){
    return this.songs;
}
public void setSongs(List songs){
    this.songs = songs;
}
 }

//

    public class Songs{
private String artist;
private String mp3;
private String song;

public String getArtist(){
    return this.artist;
}
public void setArtist(String artist){
    this.artist = artist;
}
public String getMp3(){
    return this.mp3;
}
public void setMp3(String mp3){
    this.mp3 = mp3;
}
public String getSong(){
    return this.song;
}
public void setSong(String song){
    this.song = song;
}
    }

//

public class SongsDataBase{
private Json json;

public Json getJson(){
    return this.json;
}
public void setJson(Json json){
    this.json = json;
}
 }

3 个答案:

答案 0 :(得分:1)

Google gson解析它 - 将网址加载到您的媒体播放器中,完成工作。

答案 1 :(得分:0)

Use this way to play audio songs

public class StreamAudioDemo extends Activity implements OnClickListener, OnPreparedListener, OnCompletionListener, OnErrorListener{

    private MediaPlayer mp;
    private ProgressDialog pd;
    private Button btnplay;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnplay=(Button)findViewById(R.id.play);
        btnplay.setOnClickListener(this);
    }
    @Override
    public void onPrepared(MediaPlayer mp) {
          Log.i("StreamAudioDemo", "prepare finished");
          pd.setMessage("Playing.....");
          mp.start();
    }

    @Override
    public void onClick(View v) {
        try
        {
            pd = new ProgressDialog(this);
            pd.setMessage("Buffering.....");
            pd.show();
            mp = new MediaPlayer();
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnPreparedListener(this);
            mp.setOnErrorListener(this);  //http://www.youtube.com/watch?v=pj-T_LxSCng
            mp.setDataSource("http://c2lo.reverbnation.com/audio_player/download_song_direct/9140921.mp3");

            mp.prepareAsync();
            mp.setOnCompletionListener(this);
        }
        catch(Exception e)
        {
            Log.e("StreamAudioDemo", e.getMessage());
        }

    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        pd.dismiss();
        Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();     

    }
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        // TODO Auto-generated method stub
        pd.dismiss();
        return false;
    }

}

答案 2 :(得分:0)

使用Gson。它非常简单,创建一个具有相同变量名(class,artist,...)的类(比如classB),并使用classB变量列表创建另一个类(比如classC),然后分配它

Gson gson = new Gson();

Reader r = new InputStreamReader(strJson(url));

classC C = gson.fromJson(r,ClassC.class);