Java gson中的排斥策略
我的JSON具有以下结构:Java gson中的排斥策略,java,json,gson,Java,Json,Gson,我的JSON具有以下结构: {"name": [9000, {Inst1}, ..., {Instn}]} 其中,9000是任意整数,insta是某些类的序列化实例。 我使用这样的方法将所有的Inst放入列表: Type listType = new TypeToken<ArrayList<Song>>(){}.getType(); 然后: collection = gson.fromJson(rBody, listType); 其中rBody是所有原始数组,即{“
{"name": [9000, {Inst1}, ..., {Instn}]}
其中,9000
是任意整数,insta
是某些类的序列化实例。
我使用这样的方法将所有的Inst
放入列表:
Type listType = new TypeToken<ArrayList<Song>>(){}.getType();
然后:
collection = gson.fromJson(rBody, listType);
其中rBody是所有原始数组,即{“name”:[9000,{Inst1},{Instn}]
但我得到的只是
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER`
有什么问题吗
加:
只要我知道JSON的长度永远不会超过~500,并且结构始终保持不变,那么使用下面的解决方法是否足够好
Iterator<JsonElement> it = rBody.iterator();
it.next();
while (it.hasNext()) {
collection.add(gson.fromJson(it.next(), Song.class));
}
Iterator it=rBody.Iterator();
it.next();
while(it.hasNext()){
add(gson.fromJson(it.next(),Song.class));
}
这看起来很像我在这里回答的-->
。这有帮助吗?这不是通过排除,而是通过自定义反序列化。旁注,
Class clas\u s
有时被写成Class clazz
-有人知道“标准”是什么吗@SB没有标准。这只是一个参数名……如果在应用程序中使用相同的名称,这会有所帮助,但如果不使用,我看没什么大不了的。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER`
Iterator<JsonElement> it = rBody.iterator();
it.next();
while (it.hasNext()) {
collection.add(gson.fromJson(it.next(), Song.class));
}