Java 使用GSON将json解析为pojo,pojo由列表组成
当我试图将一个json字符串解析到我的自定义类(由另一个pojo的列表组成)时,我遇到了一个问题 我得到的错误是:Java 使用GSON将json解析为pojo,pojo由列表组成,java,json,list,parsing,gson,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 { privat
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;
}
}
我已经验证了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"
}
]
}
Gson解析器显然无法将字符串直接反序列化到
文件
对象中。相反,它需要一个匹配的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;
}
}
为此:
final Gson gson = new GsonBuilder().registerTypeAdapter(File.class,
new FileTypeAdapter()).create();
这应该可以解决问题
另一方面,如果您愿意并且能够切换,则库可以处理此类反序列化,而无需特殊定制 如果您包含(一个具有代表性的示例)您试图解析的JSON,这会很有帮助。@Perception我已经包含了一个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"
}
]
}
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;
}
}
final Gson gson = new Gson();
final Gson gson = new GsonBuilder().registerTypeAdapter(File.class,
new FileTypeAdapter()).create();