我有跟随JSON(来自网址)
["Rock","Rock Argentino","Reggaeton","En Español","Reggaeton ","Reggaeton ","Boleros","Italianos ","Cumbias ","Cumbia ","Internacional","Internacional "]
你可以看到,没有" name属性"对于这些领域。
我的班级模型如下
public class KaraokeCategoryModel {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
但是GSON没有重新调整我的属性" name"
Type karaokeCollection = new TypeToken<Collection<KaraokeCategoryModel>>() {}.getType();
_karaoke_cartegory_response = new Gson().fromJson(reader, karaokeCollection);
稍后我创建了适配器
_karaoke_category_adapter = new KaraokeCategoryAdapter(getSupportActionBar().getThemedContext(), R.layout.spinner_item, _karaoke_cartegory_response);
getSupportActionBar().setListNavigationCallbacks(_karaoke_category_adapter, this);
如何让GSON毫无问题地使用我的模型?
答案 0 :(得分:1)
您可以使用以下代码更轻松地完成此操作:
List<KaraokeCategoryModel> karaokeCategoryList = new ArrayList<KaraokeCategoryModel>();
JsonElement json = new JsonParser().parse(yourJsonStringValueHere);
JsonArray jsonArray = json.getAsJsonArray();
Iterator iterator = jsonArray.iterator();
while(iterator.hasNext()){
JsonElement jsonElementInArray = (JsonElement)iterator.next();
KaraokeCategoryModel karaokeCategory = new KaraokeCategoryModel();
karaokeCategory.setName(jsonElementInArray.getAsString());
karaokeCategoryList.add(karaokeCategory);
}