Skip to content

Instantly share code, notes, and snippets.

@romrozen
Last active August 29, 2015 14:23
Show Gist options
  • Save romrozen/72385f6a1ed24d38d27c to your computer and use it in GitHub Desktop.
Save romrozen/72385f6a1ed24d38d27c to your computer and use it in GitHub Desktop.
parsing JSON with gson, model example
package com.example.test.models;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
public class SomeModel{
@SerializedName("search_results") //The exact string that will be in the parsed JSON
private SearchResults searchResults;//We see that the parsed item is an object and not a simple variable, another Model with the name "SearchResults" is created with the same procedure as in this document
public SearchResults getSearchResults() {
return searchResults;
}
public void setSearchResults(SearchResults searchResults) {
this.searchResults = searchResults;
}
}
package com.example.test.models;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class SearchResults implements Parcelable{
@SerializedName("item")
private List<Item> itemsList;
public List<Item> getItemsList() {
return itemsList;
}
public void setItemsList(List<Item> itemsList) {
this.itemsList = itemsList;
}
/* Parcel methods */
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeList(itemsList);
}
public static final Parcelable.Creator<SearchResults> CREATOR
= new Parcelable.Creator<SearchResults>() {
public SearchResults createFromParcel(Parcel in) {
return new SearchResults(in);
}
public SearchResults[] newArray(int size) {
return new SearchResults[size];
}
};
private SearchResults(Parcel in) {
itemsList = new ArrayList<Item>();
in.readList(itemsList, Item.class.getClassLoader());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment