gpt4 book ai didi

java+json : how to get an element from a json object that contains an array of objects {[ {}, {}]}

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:31 24 4
gpt4 key购买 nike

需要一些有关 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() 时,我得到“null”。

我有一个带有 String groupName 的 Record 类;字符串歌曲名称;和双倍歌曲持续时间;所有 getter 和 setter 。还有带有解析方法的 JsonParser 类。和 MusicAlbum 类与列表专辑。还有 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

我不知道该怎么做以及如何编写数据接收方法。

感谢您对此方法的任何帮助!

最佳答案

好吧,如果没有有关您的 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 有一个记录数组,每个记录都有一些字段,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());
}
}

这基本上是伪代码,它不会编译,只是为了给你一个想法。

关于java+json : how to get an element from a json object that contains an array of objects {[ {}, {}]},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com