后端:List<Musicfactory> musiclist= searchMusicDao.selectList(lqwmusic);model.addAttribute("musiclist",musiclist);前端:<input type="hidden" th:value="${musiclist}" id="currentTypeNum"/>获取后端传来的list集合该集合以对象为元素怎么在JS取出数据
时间: 2023-07-19 17:38:50 浏览: 118
你可以使用JavaScript的forEach()方法遍历这个List集合的对象元素,然后对每个对象进行属性值的获取,例如:
```javascript
var musicList = document.getElementById("currentTypeNum").value; // 获取隐藏域中的List集合
musicList.forEach(function(music) { // 遍历List集合的对象元素
var musicName = music.name; // 获取对象的name属性
var musicArtist = music.artist; // 获取对象的artist属性
// 对获取到的属性值进行操作
});
```
需要注意的是,由于获取的List集合是字符串类型,需要使用JSON.parse()方法将其转换为JavaScript的对象数组。例如:
```javascript
var musicList = JSON.parse(document.getElementById("currentTypeNum").value);
```
相关问题
package com.music.zhangdaosen; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import android.widget.ArrayAdapter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SongHelper { public static List<Song>musicList = new ArrayList<>(); // static List<Song>musicList; public static List<Song> getLocalMusic(Context context){ Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(uri,null,null,null,null); if(cursor !=null){ Song song; while (cursor.moveToNext()){ song =new Song(); int index = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME); song.name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); Log.d("SongHelper--keyar","get"+song.name+" "+song.duration); musicList.add(song); SimpleDateFormat sdf = new SimpleDateFormat("mm:ss"); String time = sdf.format(new Date(song.duration)); } Log.d("SongHelper--keyar","get musicList"+ musicList.size()); } cursor.close(); return musicList; } public static String changeTime(int duration){ int min = duration/1000/60; int sec = duration/1000%60; if(sec<10){ return min+":0"+sec; } return min+":"+sec; } }
这是一段Java代码,看起来是一个获取本地音乐列表的帮助类。它使用了Android提供的MediaStore来查询本地的音频文件,然后将查询到的结果封装成一个Song对象,并保存在一个List中。可以通过调用`getLocalMusic()`方法来获取本地音乐列表。其中,每一个Song对象代表一首音乐,包含了音乐的名称、歌手、路径、时长等信息。`changeTime()`方法用于将时间格式从毫秒转换为分秒形式。
import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; interface Music { name: string; url: string; picurl: string; artistsname: string; } @Component({ selector: 'app-music-player', templateUrl: './music-player.component.html', styleUrls: ['./music-player.component.scss'] }) export class MusicPlayerComponent { currentSong!: string; musicList: Music[] = []; currentIndex!: number; constructor(private http: HttpClient) { this.getMusicList(); } play() { const audioElement: HTMLAudioElement = document.getElementById('audio') as HTMLAudioElement; audioElement.play(); } pause() { const audioElement: HTMLAudioElement = document.getElementById('audio') as HTMLAudioElement; audioElement.pause(); } next() { this.currentIndex++; if (this.currentIndex >= this.musicList.length) { this.currentIndex = 0; } this.currentSong = this.musicList[this.currentIndex].url; const audioElement: HTMLAudioElement = document.getElementById('audio') as HTMLAudioElement; audioElement.src = this.currentSong; audioElement.play(); } getMusicList() { this.http.get<any>('https://api.uomg.com/api/rand.music?sort=热歌榜&format=json&mid=2642575625').subscribe( res => { const musicName = res.data.name; // 获取音乐名称 this.musicList = [{ name: musicName, // 将音乐名称赋值给name url: res.data.url, picurl: res.data.picurl, artistsname: res.data.artistsname }]; this.currentIndex = 0; this.currentSong = this.musicList[this.currentIndex].url; const audioElement: HTMLAudioElement = document.getElementById('audio') as HTMLAudioElement; audioElement.src = this.currentSong; }, error => { console.log('获取音乐列表失败', error); } ); } }
这是一个 Angular 组件,名为 MusicPlayerComponent。它通过 HttpClient 从一个 API 中获取音乐列表,并将其展示在页面上。这个组件包含了一些方法用于控制音乐播放,比如 play()、pause() 和 next()。在构造函数中调用了 getMusicList() 方法来获取音乐列表,并初始化了一些变量。
阅读全文