将json解析为pojo,包括列表,使用GSON

时间:2013-03-29 22:48:18

标签: java json list parsing gson

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

1 个答案:

答案 0 :(得分: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库可以处理这些类型的反序列化而无需特殊的自定义,如果您倾向于并且能够切换。

相关问题