Java GSON反序列化对象数组

时间:2017-07-10 07:19:03

标签: java json gson

我试图将JSON字符串反序列化为如下所示的对象数组:

[{"ParentLocationName":null
,"Id":"String-Id"
,"Name":"String-Name"
,"ExternalId":null
,"LocationType":0
,"TimeZone":null
,"ParentLocationId":null}
,{"ParentLocationName":"String-Name"
,"Id":"String-Id2"
,"Name":"Child-Name"
,"ExternalId":null
,"LocationType":0
,"TimeZone":null
,"ParentLocationId":"String-Id"}
,{"ParentLocationName":null
,"Id":"String-Id3"
,"Name":"Some other name"
,"ExternalId":null
,"LocationType":0
,"TimeZone":null
,"ParentLocationId":null}]

所以我有一个名为Location的类,其中包含相应的字段:

public class Location {

    public String ParentLocationName;
    public String Id;
    public String Name;
    public String ExternalId;
    public int LocationType;
    public String TimeZone;
    public String ParentLocationId;
}

我以下列方式使用Gson:

Gson gson = new Gson();
Location[] places = gson.fromJson(Json_As_String, Location[].class);

我收到错误:

Expected a string but was BEGIN_OBJECT at line 1 column 3 path $[0]

我也试过使用这样的集合:

Type collectionType = new TypeToken<>
Type colType = new TypeToken<Collection<Location>>(){}.getType();
Collection<Location> locs = gson.fromJson(Json_As_String,colType );

但收到同样的错误。

0 个答案:

没有答案