TuGordoBello
TuGordoBello

Reputation: 4509

How use GSON when JSON doen't have "Name attribute"?

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?

Upvotes: 0

Views: 123

Answers (1)

Devrim
Devrim

Reputation: 15533

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);    
}

Upvotes: 1

Related Questions