2

我正在尝试解析 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 ...

但它仍然不起作用......我错过了其他东西吗?

4

2 回答 2

0

您的字段有问题Lyrics,Gson 需要一个对象,但您提供了一个数组(仅带有一个对象)。

这是你的课:

 public class Song {
                public FileInfo   fileInfo;
                public Properties properties;
                public Lyrics    lyrics;
                // more rows

这是您无与伦比的 JSON 部分

 "lyrics": [
      //more things here      
  ]

如您所见,使用方括号,一个新数组开始。

要解决这个问题,如果你想按原样解析它,请将lyrics字段更改List<Lyrics>为,否则更改你的 JSON,如果可以的话,删除方括号。当然,您还必须更改lyric字段的 getter 和 setter。在代码方面,像这样更改:

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

这是我的执行:

    Gson gson = new Gson() ;
    SongType parsed = gson.fromJson(s, SongType.class);
    System.out.print(parsed.song.getLyrics().get(0).getLines().get(0).getText());

结果:

How <chord name="A"/>great is <br/>your love
于 2013-11-12T22:22:53.637 回答
0

这里有几个问题。

  1. 歌词是 JSON 文件中的一个数组,但您的变量不是。将其更改为List<Lyrics>
  2. 内部类Song不是静态的,这可能是一个问题(尽管显然这对您有用):请参阅此处为什么
于 2013-11-12T22:33:25.463 回答