当我尝试将 json 字符串解析到我的自定义类中时遇到问题,该类由另一个 pojo 的列表组成。
我得到的错误是:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 226
我尝试解析的类是:
package com.example.client.models;
import java.util.List;
public class Catalog {
private List<Artist> list;
public Catalog() { }
/**
* @param list
*/
public Catalog(List<Artist> list) {
super();
this.list = list;
}
/**
* @return the list
*/
public List<Artist> getList() {
return list;
}
/**
* @param list the list to set
*/
public void setList(List<Artist> list) {
this.list = list;
}
}
和艺术家类:
package com.example.client.models;
import java.io.File;
import java.util.List;
public class Artist {
private String artistName;
private List<Album> albumList;
private File artistFile;
public Artist() { }
/**
* @param artistName
* @param albumList
* @param artistFile
*/
public Artist(String artistName, List<Album> albumList, File artistFile) {
super();
this.artistName = artistName;
this.albumList = albumList;
this.artistFile = artistFile;
}
/**
* @return the artistName
*/
public String getArtistName() {
return artistName;
}
/**
* @param artistName the artistName to set
*/
public void setArtistName(String artistName) {
this.artistName = artistName;
}
/**
* @return the albumList
*/
public List<Album> getAlbumList() {
return albumList;
}
/**
* @param albumList the albumList to set
*/
public void setAlbumList(List<Album> albumList) {
this.albumList = albumList;
}
/**
* @return the artistFile
*/
public File getArtistFile() {
return artistFile;
}
/**
* @param artistFile the artistFile to set
*/
public void setArtistFile(File artistFile) {
this.artistFile = artistFile;
}
}
相册类:包 com.example.client.models;
import java.io.File;
import java.util.List;
public class Album {
private String albumName;
private List<Song> songList;
private File albumFile;
public Album() { }
/**
* @param albumName
* @param songList
* @param albumFile
*/
public Album(String albumName, List<Song> songList, File albumFile) {
super();
this.albumName = albumName;
this.songList = songList;
this.albumFile = albumFile;
}
/**
* @return the albumName
*/
public String getAlbumName() {
return albumName;
}
/**
* @param albumName the albumName to set
*/
public void setAlbumName(String albumName) {
this.albumName = albumName;
}
/**
* @return the songList
*/
public List<Song> getSongList() {
return songList;
}
/**
* @param songList the songList to set
*/
public void setSongList(List<Song> songList) {
this.songList = songList;
}
/**
* @return the albumFile
*/
public File getAlbumFile() {
return albumFile;
}
/**
* @param albumFile the albumFile to set
*/
public void setAlbumFile(File albumFile) {
this.albumFile = albumFile;
}
}
最后是歌曲类:
package com.example.client.models;
import java.io.File;
public class Song {
private String songName;
private File songFile;
public Song() { }
/**
* @param songName
* @param songFile
*/
public Song(String songName, File songFile) {
super();
this.songName = songName;
this.songFile = songFile;
}
/**
* @return the songName
*/
public String getSongName() {
return songName;
}
/**
* @param songName the songName to set
*/
public void setSongName(String songName) {
this.songName = songName;
}
/**
* @return the songFile
*/
public File getSongFile() {
return songFile;
}
/**
* @param songFile the songFile to set
*/
public void setSongFile(File songFile) {
this.songFile = songFile;
}
}
我已经验证了 json 字符串,因此我猜我的问题在于 GSON 解析。我希望你能帮我解决我的问题
如果您能举个例子或提示该怎么做,那就太好了。预先感谢!;)
编辑
嘿,我再次包含了我正在使用的 JSON 字符串的示例:
{
"list":[
{
"artistName":"tmpArtistName",
"albumList":[
{
"albumName":"tmpAlbumName",
"songList":[
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
},
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
}
],
"albumFile":"/home/pi/tmpAlbumFile"
},
{
"albumName":"tmpAlbumName",
"songList":[
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
},
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
}
],
"albumFile":"/home/pi/tmpAlbumFile"
}
],
"artistFile":"/home/pi/tmpArtistFile"
},
{
"artistName":"tmpArtistName",
"albumList":[
{
"albumName":"tmpAlbumName",
"songList":[
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
},
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
}
],
"albumFile":"/home/pi/tmpAlbumFile"
},
{
"albumName":"tmpAlbumName",
"songList":[
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
},
{
"songName":"tmpSongName",
"songFile":"/home/pi/tmpSongFile"
}
],
"albumFile":"/home/pi/tmpAlbumFile"
}
],
"artistFile":"/home/pi/tmpArtistFile"
}
]
}