Java GSON无法使用嵌套的对象数组反序列化JSON?
我这里有一个非常标准的用例:Java GSON无法使用嵌套的对象数组反序列化JSON?,java,json,serialization,gson,Java,Json,Serialization,Gson,我这里有一个非常标准的用例: { "title": "my awesome title", "description": "all the awesome things", "theThings": [ { "thing": "coolThing1", "association-type": "thing" }, { "thing": "coolThing2", "association-type": "thi
{
"title": "my awesome title",
"description": "all the awesome things",
"theThings": [
{
"thing": "coolThing1",
"association-type": "thing"
},
{
"thing": "coolThing2",
"association-type": "thing"
}
]
}
我正在利用以下类结构与GSON一起使用:
package things;
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ThingsPOJO implements Serializable
{
@SerializedName("title")
@Expose
public String title = "";
@SerializedName("description")
@Expose
public String description = "";
@SerializedName("theThings")
@Expose
public List<TheThing> theThings = null;
private class TheThing implements Serializable
{
@SerializedName("thing")
@Expose
public String thing = "";
@SerializedName("association-type")
@Expose
public String associationType = "";
}
}
打包东西;
导入java.io.Serializable;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类ThingsPOJO实现可序列化
{
@序列化名称(“标题”)
@暴露
公共字符串title=“”;
@序列化名称(“说明”)
@暴露
公共字符串description=“”;
@序列化名称(“things”)
@暴露
公共列表things=null;
私有类TheThing实现可序列化
{
@序列化名称(“事物”)
@暴露
公共字符串thing=“”;
@SerializedName(“关联类型”)
@暴露
公共字符串associationType=“”;
}
}
标题和描述很好,但对象集合为空。我正在使用上面的示例有效负载进行测试。我正在使用GSON提供的
fromJson
调用。您不需要对所有内容使用@SerializedName
和@Expose
,除非您指定通过GsonBuilder
执行此操作。您是否尝试过使用默认的空列表值初始化things
字段?如public List theThings=newarraylist()
另外,尽量不要将camelCase
与dashy case
混用。选择一个并保持一致(:我还没有,我将尝试这个。类是自动生成的,这解释了注释。它是用什么生成的?