【问题标题】:Parsing json to pojo, which consist of lists, Using GSON使用 GSON 将 json 解析为由列表组成的 pojo
【发布时间】:2013-03-20 16:57:38
【问题描述】:

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

【问题讨论】:

  • 如果您包含了您尝试解析的 JSON(代表性样本),将会很有帮助。
  • @Perception 我已经包含了一个 json 字符串的样本

标签: java json list parsing gson


【解决方案1】:

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

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-02-02
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2021-06-02
    • 2012-02-02
    相关资源
    最近更新 更多