0

別の pojo のリストで構成されるカスタム クラスに json 文字列を解析しようとすると、問題が発生します。

私が得るエラーは次のとおりです。

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;
}


 }

Album クラス: package 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 オブジェクト ( と で囲まれ{ている) が必要です。}これを回避するには、ファイル属性を処理する型アダプターを登録するだけです。

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 に答える