0

I have the follow JSON (from URL)

["Rock","Rock Argentino","Reggaeton","En Español","Reggaeton ","Reggaeton  ","Boleros","Italianos ","Cumbias ","Cumbia ","Internacional","Internacional "]

You can see that there isn't a "name attribute" for these fields.

My class model is the follow

public class KaraokeCategoryModel {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

but the GSON doesnt reconize my atribute "name"

Type karaokeCollection = new TypeToken<Collection<KaraokeCategoryModel>>() {}.getType();
        _karaoke_cartegory_response = new Gson().fromJson(reader, karaokeCollection);

later I create the adapter

_karaoke_category_adapter = new KaraokeCategoryAdapter(getSupportActionBar().getThemedContext(), R.layout.spinner_item, _karaoke_cartegory_response);
        getSupportActionBar().setListNavigationCallbacks(_karaoke_category_adapter, this);

What should I do to make GSON used my model without problems?

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78

1 Answers1

1

You can do that more easier with code below:

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);    
}
Devrim
  • 15,345
  • 4
  • 66
  • 74
  • excelent help but the aplication crash in **JsonElement json = new JsonParser().parse(URL);** can I put the URL JSON there?? – TuGordoBello Feb 23 '14 at 22:39
  • 1
    I don't think so. see how to get a json value(string) from a uri http://stackoverflow.com/questions/19966672/java-json-with-gson – Devrim Feb 23 '14 at 23:00