【发布时间】:2015-05-30 03:24:14
【问题描述】:
所以我在反序列化一些具有嵌套 ArrayLists 的 JSON 时遇到了一些问题,根元素反序列化正常,但其中的嵌套 ArrayList(称为 mTest)为空值。首先是有效的 JSON。
[
{
"mRecipeName": "FirstRecipe",
"mTest": [
{
"mIngredientName": "TestIngredient1",
"mIngredientAmount": "1",
"mUnit": "tbsp"
},
{
"mIngredientName": "TestIngredient2",
"mIngredientAmount": "2",
"mUnit": "tbsp"
}
],
"mRecipeDescription": "Recipe1"
},
{
"mRecipeName": "SecondRecipe",
"mTest": [
{
"mIngredientName": "TestIngredient1",
"mIngredientAmount": "1",
"mUnit": "tbsp"
},
{
"mIngredientName": "TestIngredient2",
"mIngredientAmount": "2",
"mUnit": "tbsp"
}
],
"mRecipeDescription": "Recipe2"
}
]
这是配方类
public class Recipe {
public String mRecipeName; //This values good
public String mRecipeDescription; //This values good
public ArrayList<Test> mTest; //This returns null
}
这是测试类
public class Test {
public String mIngredientName;
public float mIngredientAmount;
public String mUnit;
}
这就是我的称呼
//Is there another way to do this? Or does this have to be done for every nested
//ArrayList? If so how?
Gson gson = new Gson();
ArrayList<Recipe> result = gson.fromJson(json, new TypeToken<ArrayList<Recipe>>() {}.getType());
非常感谢任何回复的人!
【问题讨论】:
标签: java json arraylist nested gson