获取另一个 class 中的 arrayList 中的数据?
Get to get data from arrayList in another class?
/**
* 5 points
*
* You will write a method that converts a JSON Object into a Song object. It should assume that the input is in the format:
* {"title":"Lose Yourself", "artist":"Eminem", "ratings":[5,5,4,5], "youtubeID":"xFYQQPAOz7Y"}
*
* @param jsonSong A song in JSON format
* @return A Song object with the data from the JSON Value
*/
public static Song jsonToSong(JsonObject jsonSong){
String title =jsonSong.get("title").asString();
String YoutubeID =jsonSong.get("youtubeID").asString();
String artist =jsonSong.get("artist").asString();
ArrayList<Integer> ratings = new ArrayList<Integer>();
Song object = new Song(YoutubeID,title,artist,ratings);
object.setTitle(title);
object.setArtist(artist);
object.setRatings(ratings);
object.setYoutubeID(YoutubeID);
return object;
我编写了将 JsonObject 转换为 Song 对象类型的代码,除了 arraylist(收视率)之外的所有内容都会转换。
如何使用 getter 和 setter 方法正确检索它的数据?
我的gettersetter方法是:
public ArrayList<Integer> getRatings(){
return ratings;
}
public void setRatings(ArrayList<Integer> ratings){
this.ratings = ratings;
}
构造函数:
// Constructor
public Song(String youtubeID, String title, String artist, ArrayList<Integer> ratings){
this.youtubeID = youtubeID;
this.title = title;
this.artist = artist;
this.ratings = ratings;
}
在这里你可以看到,当 运行 通过评分器
时,除了评级之外的所有内容都发生了转换
Incorrect on input: {"title":"Comfortably Numb","artist":"Pink Floyd","ratings":[5,4,5,5],"youtubeID":"_FrOQC-zEog"}
Expected output : {title:Comfortably Numb, artist:Pink Floyd, ratings:[5, 4, 5, 5], youtubeID:_FrOQC-zEog}
Your output : {title:Comfortably Numb, artist:Pink Floyd, ratings:[], youtubeID:_FrOQC-zEog}
Score: 0
不知道你用的是什么Json库,但我猜这个Json对象有类似方法getJsonArray()的东西,你可以尝试使用它得到类型为 JsonArray 的对象,然后迭代它并得到你想要的。
或者像这样的东西
JSONArray ratings= (JSONArray) jsonSong.get("ratings");
Iterator<Integer> iterator = ratings.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
ArrayList<Integer> ratings = new ArrayList<Integer>();
JsonValue collect = jsonSong.get("ratings");
JsonArray rate = collect.asArray();
for(int i=0; i<rate.size(); i++){
ratings.add(rate.get(i).asInt());
}
/**
* 5 points
*
* You will write a method that converts a JSON Object into a Song object. It should assume that the input is in the format:
* {"title":"Lose Yourself", "artist":"Eminem", "ratings":[5,5,4,5], "youtubeID":"xFYQQPAOz7Y"}
*
* @param jsonSong A song in JSON format
* @return A Song object with the data from the JSON Value
*/
public static Song jsonToSong(JsonObject jsonSong){
String title =jsonSong.get("title").asString();
String YoutubeID =jsonSong.get("youtubeID").asString();
String artist =jsonSong.get("artist").asString();
ArrayList<Integer> ratings = new ArrayList<Integer>();
Song object = new Song(YoutubeID,title,artist,ratings);
object.setTitle(title);
object.setArtist(artist);
object.setRatings(ratings);
object.setYoutubeID(YoutubeID);
return object;
我编写了将 JsonObject 转换为 Song 对象类型的代码,除了 arraylist(收视率)之外的所有内容都会转换。
如何使用 getter 和 setter 方法正确检索它的数据?
我的gettersetter方法是:
public ArrayList<Integer> getRatings(){
return ratings;
}
public void setRatings(ArrayList<Integer> ratings){
this.ratings = ratings;
}
构造函数:
// Constructor
public Song(String youtubeID, String title, String artist, ArrayList<Integer> ratings){
this.youtubeID = youtubeID;
this.title = title;
this.artist = artist;
this.ratings = ratings;
}
在这里你可以看到,当 运行 通过评分器
时,除了评级之外的所有内容都发生了转换 Incorrect on input: {"title":"Comfortably Numb","artist":"Pink Floyd","ratings":[5,4,5,5],"youtubeID":"_FrOQC-zEog"}
Expected output : {title:Comfortably Numb, artist:Pink Floyd, ratings:[5, 4, 5, 5], youtubeID:_FrOQC-zEog}
Your output : {title:Comfortably Numb, artist:Pink Floyd, ratings:[], youtubeID:_FrOQC-zEog}
Score: 0
不知道你用的是什么Json库,但我猜这个Json对象有类似方法getJsonArray()的东西,你可以尝试使用它得到类型为 JsonArray 的对象,然后迭代它并得到你想要的。
或者像这样的东西
JSONArray ratings= (JSONArray) jsonSong.get("ratings");
Iterator<Integer> iterator = ratings.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
ArrayList<Integer> ratings = new ArrayList<Integer>();
JsonValue collect = jsonSong.get("ratings");
JsonArray rate = collect.asArray();
for(int i=0; i<rate.size(); i++){
ratings.add(rate.get(i).asInt());
}