无法使用GSON反序列化复杂的JSON对象

时间:2014-05-22 19:25:33

标签: java json gson

要使用GSON将数据从服务器传输到客户端,实际上我能够获取JSON字符串,但在反序列化时会出错。如果有人能帮助我,我会感激不尽......

JSON字符串

{ "id" : 0,
  "maxPlayers" : 0,
  "racks" : [ "EIORSSU",
      "CEKOPRV",
      "PVEELV?",
      "VAAEIR?",
      "BDENOTT",
      "DNEEIIO",
      "EEIOBLU",
      "EODFLST",
      "DSTAAEL",
      "EEIMNOS",
      "GINRRSU",
      "AEENTWX",
      "EENTIQU",
      "ADEGHJT",
      "GHCEGMU",
      "GGMAINP",
      "AGPFRTU",
      "FGANORY",
      "FGNORLM",
      "GLMNHIS",
      "HLMNANZ",
      "AHLMN",
      "ALM",
      "L"
    ],
  "strokes" : [ { "2H:\"null\"" : "R",
        "3H:\"null\"" : "O",
        "4H:\"null\"" : "U",
        "5H:\"null\"" : "I",
        "6H:\"null\"" : "S",
        "7H:\"null\"" : "S",
        "8H:\"null\"" : "E"
      },
      { "2C:\"null\"" : "R",
        "2D:\"null\"" : "O",
        "2E:\"null\"" : "C",
        "2F:\"null\"" : "K",
        "2G:\"null\"" : "E",
        "2H:\"null\"" : "R"
      },
      { "4G:\"null\"" : "P",
        "5G:\"null\"" : "L",
        "6G:\"null\"" : "E",
        "7G:\"null\"" : "u",
        "8G:\"null\"" : "V",
        "9G:\"null\"" : "E"
      },
      { "10F:\"null\"" : "E",
        "11F:\"null\"" : "R",
        "12F:\"null\"" : "A",
        "13F:\"null\"" : "I",
        "7F:\"null\"" : "l",
        "8F:\"null\"" : "A",
        "9F:\"null\"" : "V"
      },
      { "14D:\"null\"" : "B",
        "14E:\"null\"" : "O",
        "14F:\"null\"" : "T",
        "14G:\"null\"" : "T",
        "14H:\"null\"" : "E"
      },
      { "10E:\"null\"" : "D",
        "8E:\"null\"" : "N",
        "9E:\"null\"" : "I"
      },
      { "3A:\"null\"" : "B",
        "3B:\"null\"" : "L",
        "3C:\"null\"" : "E",
        "3D:\"null\"" : "U",
        "3E:\"null\"" : "I"
      },
      { "10D:\"null\"" : "O",
        "11D:\"null\"" : "F",
        "12D:\"null\"" : "E",
        "9D:\"null\"" : "L"
      },
      { "13H:\"null\"" : "D",
        "13I:\"null\"" : "E",
        "13J:\"null\"" : "T",
        "13K:\"null\"" : "A",
        "13L:\"null\"" : "L",
        "13M:\"null\"" : "A",
        "13N:\"null\"" : "S"
      },
      { "10L:\"null\"" : "I",
        "11L:\"null\"" : "N",
        "12L:\"null\"" : "O",
        "13L:\"null\"" : "L",
        "14L:\"null\"" : "E",
        "7L:\"null\"" : "S",
        "8L:\"null\"" : "E",
        "9L:\"null\"" : "M"
      },
      { "2I:\"null\"" : "S",
        "3I:\"null\"" : "U",
        "4I:\"null\"" : "R"
      },
      { "6M:\"null\"" : "W",
        "7M:\"null\"" : "A",
        "8M:\"null\"" : "X"
      },
      { "10H:\"null\"" : "I",
        "10I:\"null\"" : "N",
        "10J:\"null\"" : "Q",
        "10K:\"null\"" : "U",
        "10L:\"null\"" : "I",
        "10M:\"null\"" : "E",
        "10N:\"null\"" : "T",
        "10O:\"null\"" : "E"
      },
      { "10O:\"null\"" : "E",
        "11O:\"null\"" : "T",
        "12O:\"null\"" : "A",
        "7O:\"null\"" : "D",
        "8O:\"null\"" : "E",
        "9O:\"null\"" : "J"
      },
      { "3A:\"null\"" : "B",
        "4A:\"null\"" : "U",
        "5A:\"null\"" : "C",
        "6A:\"null\"" : "H",
        "7A:\"null\"" : "E"
      },
      { "10C:\"null\"" : "M",
        "11C:\"null\"" : "I",
        "12C:\"null\"" : "N",
        "13C:\"null\"" : "G"
      },
      { "4K:\"null\"" : "P",
        "5K:\"null\"" : "A",
        "6K:\"null\"" : "R",
        "7K:\"null\"" : "U",
        "8K:\"null\"" : "T"
      },
      { "11G:\"null\"" : "A",
        "12G:\"null\"" : "Y"
      },
      { "5J:\"null\"" : "F",
        "6J:\"null\"" : "O",
        "7J:\"null\"" : "R"
      },
      { "4C:\"null\"" : "G",
        "4D:\"null\"" : "I",
        "4E:\"null\"" : "S"
      },
      { "12I:\"null\"" : "N",
        "13I:\"null\"" : "E",
        "14I:\"null\"" : "Z"
      },
      { "12M:\"null\"" : "H",
        "13M:\"null\"" : "A",
        "14M:\"null\"" : "N"
      },
      { "1B:\"null\"" : "M",
        "1C:\"null\"" : "A"
      }
    ],
  "timeout" : 0
}

目标类:

Class Game {
        private int id;
    private String name;
    private int timeout;
    private int maxPlayers;
    private List<String> racks;
    private List<LinkedHashMap<Position, Character>> strokes;
    private List<Player> players;}

我收到了这个错误:

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT但在第1行第287行为STRING       在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:176)       在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)       在com.google.gson.internal.bind.MapTypeAdapterFactory $ Adapter.read(MapTypeAdapterFactory.java:186)       在com.google.gson.internal.bind.MapTypeAdapterFactory $ Adapter.read(MapTypeAdapterFactory.java:145)       在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)       在com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:81)       在com.google.gson.internal.bind.CollectionTypeAdapterFactory $ Adapter.read(CollectionTypeAdapterFactory.java:60)       在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.read(ReflectiveTypeAdapterFactory.java:93)       在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:172)       在com.google.gson.Gson.fromJson(Gson.java:803)       在com.google.gson.Gson.fromJson(Gson.java:768)       在com.google.gson.Gson.fromJson(Gson.java:717)       在com.google.gson.Gson.fromJson(Gson.java:689)       at fr.univtln.groupec.dupliword_server.main.Serveur.main(Serveur.java:87)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:606)       在org.codehaus.mojo.exec.ExecJavaMojo $ 1.run(ExecJavaMojo.java:293)       在java.lang.Thread.run(Thread.java:745)   引起:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第287行STRING       在com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)       在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:165)       ......还有19个

我认为这个问题来自&#34; List&gt;&#34;有人可以告诉我如何反序列化?

  

[{&#34; 2H:\&#34;空\&#34;&#34;:&#34; R&#34;&#34; 3H:\&#34;空\& #34;&#34;:&#34; O&#34;&#34; 4H:\&#34;空\&#34;&#34;:&#34; U&#34;&# 34; 5H:\&#34;空\&#34;&#34;:&#34; I&#34;&#34; 6H:\&#34;空\&#34;&#34; :&#34; S&#34;&#34; 7H:\&#34;空\&#34;&#34;:&#34; S&#34;&#34; 8H:\&# 34;空\&#34;&#34;:&#34; E&#34;},{&#34; 2C:\&#34;空\&#34;&#34;:&#34; R&#34;&#34; 2D:\&#34;空\&#34;&#34;:&#34; O&#34;&#34; 2E:\&#34;空\& #34;&#34;:&#34; C&#34;&#34; 2F:\&#34;空\&#34;&#34;:&#34; K&#34;&# 34; 2G:\&#34;空\&#34;&#34;:&#34; E&#34;&#34; 2H:\&#34;空\&#34;&#34; :&#34; R&#34;},{&#34; 4G:\&#34;空\&#34;&#34;:&#34; P&#34;&#34; 5G:\ &#34;空\&#34;&#34;:&#34; L&#34;&#34; 6G:\&#34;空\&#34;&#34;:&#34; E&#34;&#34; 7G:\&#34;空\&#34;&#34;:&#34; U&#34;&#34; 8G:\&#34;空\& #34;&#34;:&#34; V&#34;&#34; 9G:\&#34;空\&#34;&#34;:&#34; E&#34;},{ &#34; 7F:\&#34;空\&#34;&#34;:&#34; L&#34;&#34; 8F:\&#34;空\&#34;&# 34;:&#34; A&#34;&#34;图9F:\&#34;空\&#34;&#34;:&#34; V&#34;&#34; 10F:\ &#34;空\&#34;&#34;:&#34; E&#34;&#34; 11F:\&#34;空\&# 34;&#34;:&#34; R&#34;&#34; 12F:\&#34;空\&#34;&#34;:&#34; A&#34;&#34 ; 13F:\&#34;空\&#34;&#34;:&#34; I&#34;},{&#34; 14D:\&#34;空\&#34;&#34 ;:&#34; B&#34;&#34; 14E:\&#34;空\&#34;&#34;:&#34; O&#34;&#34; 14F:\& #34;空\&#34;&#34;:&#34; T&#34;&#34; 14G:\&#34;空\&#34;&#34;:&#34; T& #34;&#34; 14H:\&#34;空\&#34;&#34;:&#34; E&#34;},{&#34; 8E:\&#34;空\ &#34;&#34;:&#34; N&#34;&#34; 9E:\&#34;空\&#34;&#34;:&#34; I&#34;,& #34; 10E:\&#34;空\&#34;&#34;:&#34; d&#34;},{&#34; 3A:\&#34;空\&#34;& #34;:&#34; B&#34;&#34; 3B:\&#34;空\&#34;&#34;:&#34; L&#34;&#34; 3C: \&#34;空\&#34;&#34;:&#34; E&#34;&#34; 3D:\&#34;空\&#34;&#34;:&#34 ; U&#34;&#34; 3E:\&#34;空\&#34;&#34;:&#34; I&#34;},{&#34; 9D:\&#34;空\&#34;&#34;:&#34; L&#34;&#34; 10D:\&#34;空\&#34;&#34;:&#34; O&#34; &#34; 11D:\&#34;空\&#34;&#34;:&#34; F&#34;&#34; 12D:\&#34;空\&#34;& #34;:&#34; E&#34;},{&#34; 13H:\&#34;空\&#34;&#34;:&#34; d&#34;&#34; 13I:\&#34;空\&#34;&#34;:&#34; E&#34;&#34; 13J:\&#34;空\&#34;&#34;:& #34; T&#34;&#34; 13K:\&#34;空\&#34;&#34 ;: &#34; A&#34;&#34; 13L:\&#34;空\&#34;&#34;:&#34; L&#34;&#34; 13M:\&#34 ;空\&#34;&#34;:&#34; A&#34;&#34; 13N:\&#34;空\&#34;&#34;:&#34; S&#34 ;},{&#34; 7L:\&#34;空\&#34;&#34;:&#34; S&#34;&#34; 8L:\&#34;空\&# 34;&#34;:&#34; E&#34;&#34; 9L:\&#34;空\&#34;&#34;:&#34; M&#34;&#34 ; 10L:\&#34;空\&#34;&#34;:&#34; I&#34;&#34; 11L:\&#34;空\&#34;&#34 ;: &#34; N&#34;&#34; 12L:\&#34;空\&#34;&#34;:&#34; O&#34;&#34; 13L:\&#34 ;空\&#34;&#34;:&#34; L&#34;&#34; 14L:\&#34;空\&#34;&#34;:&#34; E&#34 ;},{&#34; 2I:\&#34;空\&#34;&#34;:&#34; S&#34;&#34; 3I:\&#34;空\&# 34;&#34;:&#34; U&#34;&#34; 4I:\&#34;空\&#34;&#34;:&#34; R&#34;},{& #34; 6M:\&#34;空\&#34;&#34;:&#34; W&#34;&#34; 7M:\&#34;空\&#34;&#34 ;:&#34; A&#34;&#34; 8M:\&#34;空\&#34;&#34;:&#34; X&#34;},{&#34; 10H: \&#34;空\&#34;&#34;:&#34; I&#34;&#34; 10I:\&#34;空\&#34;&#34;:&#34 ; N&#34;&#34; 10J:\&#34;空\&#34;&#34;:&#34; Q&#34;&#34; 10K:\&#34;空\ &#34;&#34;:&#34; U&#34;&#34; 10L:\&#34;空\&#34;&#34;:&#34; I&#34;,& #34; 10M:\&#34;空\&#34;&#34;:&#34; E&#34 ;, &#34; 10N:\&#34;空\&#34;&#34;:&#34; T&#34;&#34; 10O:\&#34;空\&#34;&# 34;:&#34; E&#34;},{&#34; 70:\&#34;空\&#34;&#34;:&#34; d&#34;&#34; 80 :\&#34;空\&#34;&#34;:&#34; E&#34;&#34; 90:\&#34;空\&#34;&#34;:&# 34; J&#34;&#34; 10O:\&#34;空\&#34;&#34;:&#34; E&#34;&#34; 11O:\&#34;空\&#34;&#34;:&#34; T&#34;&#34; 120:\&#34;空\&#34;&#34;:&#34; A&#34;} ,{&#34; 3A:\&#34;空\&#34;&#34;:&#34; B&#34;&#34; 4A:\&#34;空\&#34; &#34;:&#34; U&#34;&#34; 5A:\&#34;空\&#34;&#34;:&#34; C&#34;&#34;图6A :\&#34;空\&#34;&#34;:&#34; H&#34;&#34; 7A:\&#34;空\&#34;&#34;:&# 34; E&#34;},{&#34; 10C:\&#34;空\&#34;&#34;:&#34; M&#34;&#34; 11C:\&#34 ;空\&#34;&#34;:&#34; I&#34;&#34; 12C:\&#34;空\&#34;&#34;:&#34; N&#34 ;,&#34; 13 C:\&#34;空\&#34;&#34;:&#34; G&#34;},{&#34; 4K:\&#34;空\&# 34;&#34;:&#34; P&#34;&#34; 5K:\&#34;空\&#34;&#34;:&#34; A&#34;&#34 ; 6K:\&#34;空\&#34;&#34;:&#34; R&#34;&#34; 7K:\&#34;空\&#34;&#34 ;: &#34; U&#34;&#34; 8K:\&#34;空\&#34;&#34;:&#34; T&#34;},{&#34; 11G:\& #34;空\&#34;&#34;:&#34; A&#34;&#34; 12G:\&#34 ;空\&#34;&#34;:&#34; Y&#34;},{&#34; 5J:\&#34;空\&#34;&#34;:&#34; F& #34;&#34; 6J:\&#34;空\&#34;&#34;:&#34; O&#34;&#34; 7J:\&#34;空\&# 34;&#34;:&#34; R&#34;},{&#34;图4C:\&#34;空\&#34;&#34;:&#34; G&#34;,& #34;图4D:\&#34;空\&#34;&#34;:&#34; I&#34;&#34; 4E:\&#34;空\&#34;&#34 ;:&#34; S&#34;},{&#34; 12I:\&#34;空\&#34;&#34;:&#34; N&#34;&#34; 13I: \&#34;空\&#34;&#34;:&#34; E&#34;&#34; 14I:\&#34;空\&#34;&#34;:&#34 ; Z&#34;},{&#34; 12M:\&#34;空\&#34;&#34;:&#34; H&#34;&#34; 13M:\&#34;空\&#34;&#34;:&#34; A&#34;&#34; 14M:\&#34;空\&#34;&#34;:&#34; N&#34; },{&#34; 1B:\&#34;空\&#34;&#34;:&#34; M&#34;&#34; 1C:\&#34;空\&#34 ;&#34;:&#34; A&#34;}]

2 个答案:

答案 0 :(得分:1)

我看到一个明显的问题和一些其他潜在的杂项问题:

  1. You can't so easily deserialize generic types. You have to work a little harder to deserialize them, click this link.特别是,我指的是List<LinkedHashMap<Position, Character>>
  2. 以下是其他潜在问题:

    1. 您没有告诉我们Position类是什么,它可能不匹配
    2. Json字符串中没有players的条目

答案 1 :(得分:0)

您可以尝试使用

private List<LinkedHashMap<String, Character>> strokes;

而不是

private List<LinkedHashMap<Position, Character>> strokes;

在字符串"2H:\"null\"" : "R"中,它将2H:"null"视为关键字,将R视为地图的值。