0

当我尝试将 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"
  }
]
}
4

1 回答 1

1

Gson 解析器显然无法将字符串直接反序列化为File对象。{相反,它需要一个匹配的 JSON 对象(由and包围})才能继续解组。为了解决这个问题,您可以简单地注册一个类型适配器来处理文件属性:

public class FileTypeAdapter extends TypeAdapter<File> {

    @Override
    public void write(final JsonWriter out, final File value)
            throws IOException {
        if (value == null) {
            out.nullValue();
        } else {
            out.value(value.getAbsolutePath());
        }
    }

    @Override
    public File read(final JsonReader in) throws IOException {
        if (in.hasNext()) {
            final String name = in.nextString();
            return name != null ? new File(name) : null;
        }

        return null;
    }
}

要使用它,其余代码将保持不变,但替换 Gson 解析器的创建:

final Gson gson = new Gson();

有了这个:

final Gson gson = new GsonBuilder().registerTypeAdapter(File.class,
    new FileTypeAdapter()).create();

那应该可以解决问题。


附带说明一下,如果您愿意并且能够切换,Jackson库无需特殊定制即可处理此类反序列化。

于 2013-03-30T00:05:07.870 回答