1
嗨,我的json是這樣的,我用gson轉換爲Java,我創建了具有所有屬性的頂級類和具有列表的另一個類。但是,即使數組大小正確,值也會打印爲空。我究竟做錯了什麼?用於反序列化嵌套JSON的GSON代碼
Gson json = new Gson();
ProductListResponse [] response = json.fromJson(str, ProductListResponse[].class);
public class ProductList {
public String code;
public String name;
public String description;
public Date lastUpdateDate;
public String lastUpdatedBy;
}
------------------------------------------------------------------------------------
public class ProductListResponse {
public ProductList plist;
}
-------------------------------------------------------------------------------------------
[
{
"product": {
"code": "RA",
"name": "Read About",
"description": "Read About"
}
},
{
"product": {
"code": "READ180",
"name": "Read180",
"description": "Read180"
}
},
{
"product": {
"code": "RLIB",
"name": "Read180 Library Catalog",
"description": "Read180 Library Catalog",
"last_udapte_date": "2010-12-07 00:00:00.0",
"last_updated_by": "fdixon00"
}
},
{
"product": {
"code": "EREADS",
"name": "Read180 eReads",
"description": "Read180 eReads"
}
},
{
"product": {
"code": "RSKILL",
"name": "Read180 rSkills",
"description": "Read180 rSkills",
"last_udapte_date": "2010-09-20 00:00:00.0",
"last_updated_by": "fdixon00"
}
},
{
"product": {
"code": "POOL",
"name": "Reference Asset Pool",
"description": "Reference Asset Pool"
}
},
{
"product": {
"code": "SU",
"name": "Scholastic U",
"description": "Scholastic U"
}
},
{
"product": {
"code": "TR8TS",
"name": "Six Traits of Writing",
"description": "Six Traits of Writing"
}
},
{
"product": {
"code": "TFX",
"name": "TrueFlix",
"description": "TrueFlix",
"last_udapte_date": "2012-08-20 10:47:46.0",
"last_updated_by": "wbyler00"
}
}
]
太棒了,它的工作。我可以知道爲什麼需要這麼做嗎? –
@Sri Ram:字段的名稱必須與JSON中的名稱匹配,這就是全部原因。如果您需要或需要不同的名稱,請查看[NamingStrategy](http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/GsonBuilder.html#setFieldNamingStrategy (com.google.gson.FieldNamingStrategy)。如果問題解決了,請隨時接受我的答案。:D – maaartinus