Created
July 15, 2013 09:27
-
-
Save kmb385/5998632 to your computer and use it in GitHub Desktop.
This is an example for StackOverflow to help someone resolve a GSON issue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Type; | |
import java.util.List; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
public class MapData { | |
private int id; | |
private String city; | |
private String street; | |
private String state; | |
private int zipcode; | |
private double lat; | |
private double lng; | |
public MapData(int id, String city, String street, String state, | |
int zipcode, double lat, double lng) { | |
this.id = id; | |
this.city = city; | |
this.street = street; | |
this.state = state; | |
this.zipcode = zipcode; | |
this.lat = lat; | |
this.lng = lng; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getCity() { | |
return city; | |
} | |
public void setCity(String city) { | |
this.city = city; | |
} | |
public String getStreet() { | |
return street; | |
} | |
public void setStreet(String street) { | |
this.street = street; | |
} | |
public String getState() { | |
return state; | |
} | |
public void setState(String state) { | |
this.state = state; | |
} | |
public int getZipcode() { | |
return zipcode; | |
} | |
public void setZipcode(int zipcode) { | |
this.zipcode = zipcode; | |
} | |
public double getLat() { | |
return lat; | |
} | |
public void setLat(double lat) { | |
this.lat = lat; | |
} | |
public double getLng() { | |
return lng; | |
} | |
public void setLng(double lng) { | |
this.lng = lng; | |
} | |
public static void main(String[] args) { | |
List<MapData> data = getData(); | |
System.out.println(data.get(0).city); | |
} | |
public static List<MapData> getData(){ | |
Gson gson = new Gson(); | |
String jsonString = "[{\"id\":18,\"city\":\"test\",\"street\":\"test 1\",\"zipcode\":121209,\"state\":\"IL\",\"lat\":32.158138,\"lng\":34.807838},{\"id\":19,\"city\":\"test\",\"street\":\"1\",\"zipcode\":76812,\"state\":\"IL\",\"lat\":32.161041,\"lng\":34.810410}]"; | |
Type type = new TypeToken<List<MapData>>(){}.getType(); | |
return gson.fromJson(jsonString, type); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment