我正在尝试解析 JSon 文件,但我无法弄清楚我的错误(较小的示例可以完美地工作)
歌曲.json
{
"song": {
"fileInfo": {
"version": "0.1",
"createdIn": "PickWorks Studio",
"modifiedIn": "PickWorks Studio",
"modified": "2010-01-28T13:15:30+01:00"
},
"properties": {
"authors": [
{
"name": "Juri Traktori",
"type": "music",
"lang": "en"
}
],
"titles": [
{
"title": "Rainy day",
"lang": "en",
"original": true
},
{
"title": "Descowe dni",
"lang": "pl"
}
],
"copywright": "Michal Tomanek",
"released": "19-03-1993",
"transposition": -3,
"tempo": {
"type": "text/bpm",
"value": "moderate/90"
},
"key": "A",
"version": 0.99,
"publisher": "myself",
"keywords": [ "grace", "words","amazing"],
"verseOrder": "v1 v2 v1 v1 v2",
"themes": [
{
"theme": "adoration"
}
]
},
"lyrics": [
{
"section": "v1",
"lines": [
{
"lang": "en",
"part": "man",
"text": "How <chord name=\"A\"/>great is <br/>your love",
"comment": "Sing softly"
},
{
"lang": "en",
"part": "woman",
"text": "How great is your love to us"
}
]
}
]
}
}
SongType.java:(相当长)http://pastebin.com/uaEY7dty
当我像往常一样做时:
Gson gson = new Gson() ;
SongType parsed = gson.fromJson(json, SongType.class);
它崩溃了......我被困了好几天,无法把它弄好。
顺便说一句:这是我关于 SO 的第一个问题,所以如果它没有按应有的方式呈现,请原谅。
编辑:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 692
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at Main.main(Main.java:14)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 692
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
编辑2:
1) 歌曲现在是静态的 2) 歌词在列表内(里面有更多部分)
编辑 3:
import java.util.List;
public class SongType {
public static Song song;
public class Song {
public FileInfo fileInfo;
public Properties properties;
public List<Lyrics> lyrics;
public FileInfo getFileInfo() {return fileInfo;}
public Properties getProperties() {return properties;}
public List<Lyrics> getLyrics() {return lyrics;}
public void setFileInfo(FileInfo fileInfo) {this.fileInfo = fileInfo;}
public void setProperties(Properties properties) {this.properties = properties;}
public void setLyrics(List<Lyrics> lyrics) {this.lyrics = lyrics;}
}
//code continues here ...
但它仍然不起作用......我错过了其他东西吗?