我在我的项目中使用Jackson来解析从服务器收到的JSON,解析步骤非常好,在尝试使用嵌套对象时会出现问题。这是一些代码:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Song {
@JsonProperty("id")
private int id = 0;
@JsonProperty("title")
private String title = "";
@JsonProperty("link")
private String link = "";
@JsonProperty("album")
private Album album;
public Song() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link ;
}
public void setLink(String link ) {
this.link = link ;
}
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
}
Album类看起来像这样:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Album {
@JsonProperty("id")
private int id;
@JsonProperty("title")
private String title;
@JsonProperty("artist")
private Artist artist;
public Album(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title){
this.title = title;
}
public Artist getArtist() {
return artist;
}
public void setArtist(Artist artist) {
this.artist = artist;
}
}
最后是“艺术家”课程:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Artist{
@JsonProperty("id")
private int id;
@JsonProperty("name")
private String name;
public Artist() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
所以,正如我在JSON解析之前所说的那样,问题是当我尝试访问其中一个“子”对象时,如果我试图获取艺术家名称。当我这样做时:
getSong().getAlbum().getArtist().getName();
无论我在哪里做,杰克逊停止解析JSON给出:
(IOException)Error: No suitable constructor found for type [simple type, class com.zarpamos.fandanztap.model.Album]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
普通JSON输出:
{"categories":[{"id":"1","name":"Pop","packs":[{"id":"2","songs":[{"album":{"id":"2","title":"Sex And Love","artist":{"id":"2","name":"Enrique Iglesias"}},"id":"3","title":"Bailando","link":"song_1.mp3"},{"album":{"id":"1","title":"Peace Is The Mission","artist":{"id":"3","name":"Major Lazer"}},"id":"6","title":"Powerful","link":"song_2.mp3"},{"album":{"id":"2","title":"Sex And Love","artist":{"id":"2","name":"Enrique Iglesias"}},"id":"4","title":"Loco","link":"song_3.mp3"}, // More JSON elements...
解析“类别”和“包”非常有效,问题出在“专辑”中。
相同的JSON输出但风格化:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Pop
[packs] => Array
(
[0] => Array
(
[id] => 2
[songs] => Array // Here starts the songs JSON array, where the error is produced
(
[0] => Array // song
(
[album] => Array // album
(
[id] => 2
[title] => Sex And Love
[artist] => Array // artist
(
[id] => 2
[name] => Enrique Iglesias
)
)
[id] => 3
[title] => Bailando
[link] => song_1.mp3
)
[1] => Array
(
[album] => Array
(
[id] => 1
[title] => Peace Is The Mission
[artist] => Array
(
[id] => 3
[name] => Major Lazer
)
)
[id] => 6
[title] => Powerful
[link] => song_2.mp3
)
[2] => Array
(
[album] => Array
(
[id] => 2
[title] => Sex And Love
[artist] => Array
(
[id] => 2
[name] => Enrique Iglesias
)
)
[id] => 4
[title] => Loco
[link] => song_3.mp3
)
// More array elements...
这是堆栈跟踪:
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1080)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:142)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248)
W/System.err﹕ at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
W/System.err﹕ at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2578)
W/System.err﹕ at com.dolphinziyo.myapp.tasks.GetPacksTask.onPostExecute(GetPacksTask.java:93)
W/System.err﹕ at com.dolphinziyo.myapp.tasks.GetPacksTask.onPostExecute(GetPacksTask.java:38)
W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:631)
W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:107)
W/System.err﹕ at android.os.Looper.loop(Looper.java:194)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5371)
W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
这真的很奇怪,我不知道我做错了什么。谁能告诉我错误在哪里?
提前谢谢
答案 0 :(得分:0)
在Album
课程中,您必须拥有Artist
属性的设置器,如下所示:
public void setArtist(Artist artist) {
this.artist = artist;
}
不喜欢这样:
public void setArtist(**Artista** artist) {
this.artist = artist;
}
答案 1 :(得分:0)
我终于手动解析它,直到我有更多时间来测试它。
答案 2 :(得分:0)
您的类是顶级类型还是嵌套在另一个类中?如果他们已经嵌套,您需要将其标记为“静态”