需要有关java + json的帮助。 大家好!我需要按歌曲标题对数据进行排序,并提供特殊情况的处理。我需要编写对json文件中的数据进行排序的方法。例如,此方法还必须能够对具有相同字段的数据进行排序。该文件的解析有效。但是它的结构如下:
{
"musicAlbum": [
{
"groupname": "twenty one pilots",
"songduration": 3.27,
"songname": "Heathens"
},
{
"groupname": "twenty one pilots",
"songduration": 4.4,
"songname": "Car Radio"
},
{
"groupname": "Linkin Park",
"songduration": 3.06,
"songname": "Numb"
}
]
}
我现在不怎么从对象数组中获取任何元素。例如,当我尝试执行以下操作:.getSongName()时,我得到“空”。
我有带字符串groupName的Record类;字符串songName;和double songDuration;所有获取器和设置器。另外JsonParser类具有解析方法。和MusicAlbum类一起使用List专辑。并与main一起上课:
import java.io.IOException;
import java.util.List;
public class App {
public static void main(String[] args) throws IOException // exception to be
handled
{
List<Record> album = JsonParser.parseJson();
System.out.println(album);
for (int i = 0; i < album.size(); i++) {
System.out.println(album.get(i));
}
Record songName = new Record();
System.out.println(songName);
System.out.println(songName.getSongName());
}
}
我现在在控制台中拥有什么:
[MusicAlbum [album=[Record [groupName=twenty one pilots, songName=Heathens, songDuration=3.27] , Record [groupName=twenty one pilots, songName=Car Radio, songDuration=4.4] , Record [groupName=Linkin Park, songName=Numb, songDuration=3.06] , Record [groupName=Lana Del Rey, songName=Summertime sadness, songDuration=3.56] , Record [groupName=Imagine Dragons, songName=Thunder, songDuration=3.24] , Record [groupName=Three Days Grace, songName=Outsider, songDuration=2.43] , Record [groupName=ONUKA, songName=When I Met You, songDuration=4.04] , Record [groupName=Foster The People, songName=Best Friend, songDuration=4.25] , Record [groupName=Massive Attack, songName=Angel, songDuration=6.18] , Record [groupName=Florence + The Machine, songName=Big God, songDuration=4.28] , Record [groupName=Die antwoord, songName=banana brain, songDuration=7.12] , Record [groupName=Coldplay, songName=Hypnotised, songDuration=5.54] ]]]
MusicAlbum [album=[Record [groupName=twenty one pilots, songName=Heathens, songDuration=3.27] , Record [groupName=twenty one pilots, songName=Car Radio, songDuration=4.4] , Record [groupName=Linkin Park, songName=Numb, songDuration=3.06] , Record [groupName=Lana Del Rey, songName=Summertime sadness, songDuration=3.56] , Record [groupName=Imagine Dragons, songName=Thunder, songDuration=3.24] , Record [groupName=Three Days Grace, songName=Outsider, songDuration=2.43] , Record [groupName=ONUKA, songName=When I Met You, songDuration=4.04] , Record [groupName=Foster The People, songName=Best Friend, songDuration=4.25] , Record [groupName=Massive Attack, songName=Angel, songDuration=6.18] , Record [groupName=Florence + The Machine, songName=Big God, songDuration=4.28] , Record [groupName=Die antwoord, songName=banana brain, songDuration=7.12] , Record [groupName=Coldplay, songName=Hypnotised, songDuration=5.54] ]]
Record [groupName=null, songName=null, songDuration=0.0]
null
我不知道要怎么做以及如何编写数据接收方法。
感谢您提供有关此方法的帮助!
答案 0 :(得分:0)
您没有将任何值添加到songName对象:
Record songName = new Record(); // you should add values to "songName" object
System.out.println(songName);
System.out.println(songName.getSongName());
因此,所有内容均为空。 尝试做这样的事情:
将响应“ musicAlbum”添加到JSONArray对象,然后遍历该数组并获取每条记录。
List<Record> records = new ArrayList<>();
Record record = new Record();
for (int i=0;i<jsonArray.length();i++) {
JSONObject jObj = jsonArray.getJSONObject(i);
record.setGroupName = jObj.getString("groupname");
record.setSongName = jObj.getString("songname");
record.setSongDuration = jObj.getString("songduration");
records.add(record);
}
答案 1 :(得分:0)
好的,没有关于您的Record
类和解析器的更多详细信息,我只能进行一些反向工程。
这就是您的数据的样子。
MusicAlbum [
album= [
Record [groupName=twenty one pilots, songName=Heathens, songDuration=3.27] ,
Record [groupName=twenty one pilots, songName=Car Radio, songDuration=4.4] ,
Record [groupName=Linkin Park, songName=Numb, songDuration=3.06] ,
Record [groupName=Lana Del Rey, songName=Summertime sadness, songDuration=3.56] ,
Record [groupName=Imagine Dragons, songName=Thunder, songDuration=3.24] ,
Record [groupName=Three Days Grace, songName=Outsider, songDuration=2.43] ,
Record [groupName=ONUKA, songName=When I Met You, songDuration=4.04] ,
Record [groupName=Foster The People, songName=Best Friend, songDuration=4.25] ,
Record [groupName=Massive Attack, songName=Angel, songDuration=6.18] ,
Record [groupName=Florence + The Machine, songName=Big God, songDuration=4.28] ,
Record [groupName=Die antwoord, songName=banana brain, songDuration=7.12] , Record [groupName=Coldplay, songName=Hypnotised, songDuration=5.54]
]
]
MusicAlbum
有一个records
数组,每个记录都有一些字段,songName
是其中之一。
因此,获取记录详细信息的总体算法如下:
List<Album> albums = parseJson("{...}");
for (Album album : albums) {
// 'album' is a bad name here, it's storing records, not albums
List<Record> records = album.getAlbum();
for (Record record : records) {
System.out.println(record.getSonName());
}
}
这基本上是伪代码,不会编译,只是给您一个想法。