Last active
December 30, 2015 17:39
-
-
Save jonikarppinen/7862574 to your computer and use it in GitHub Desktop.
Answer for this question about Gson: http://stackoverflow.com/questions/20442265/how-to-decode-json-with-unknown-field-using-gson. This demonstrates how to use custom deserialiser to parse a big list of properties of the same type but unknown/random/varying names. Besides JDK, depends on Guava library.
This file contains 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 com.google.common.base.Charsets; | |
import com.google.common.io.Files; | |
import com.google.gson.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.lang.reflect.Type; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES; | |
/** | |
* http://stackoverflow.com/questions/20442265/how-to-decode-json-with-unknown-field-using-gson | |
* | |
* @author: jonik, http://stackoverflow.com/users/56285/jonik | |
* Date: 7.12.2013 | |
*/ | |
public class Question20442265 { | |
public static void main(String[] args) throws IOException { | |
// using this test JSON: https://www.dropbox.com/s/a89s0ysv2cr8w44/question20442264.json?dl=0 | |
File jsonFile = new File("src/gson/", "question20442264.json"); | |
String json = Files.toString(jsonFile, Charsets.UTF_8); | |
GsonBuilder builder = new GsonBuilder(); | |
builder.registerTypeAdapter(CityList.class, new CityListDeserializer()); | |
Gson gson = builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create(); | |
CityList list = gson.fromJson(json, CityList.class); | |
System.out.println(list.cities); | |
} | |
private static class CityList { | |
List<City> cities; | |
private CityList(List<City> cities) { | |
this.cities = cities; | |
} | |
} | |
private static class City { | |
String citiesId; | |
String city; | |
String regionName; | |
// and so on | |
@Override | |
public String toString() { | |
return "City{" + | |
"citiesId='" + citiesId + '\'' + | |
", city='" + city + '\'' + | |
", regionName='" + regionName + '\'' + | |
'}'; | |
} | |
} | |
private static class CityListDeserializer implements JsonDeserializer<CityList> { | |
@Override | |
public CityList deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { | |
JsonObject object = jsonElement.getAsJsonObject(); | |
List<City> cities = new ArrayList<City>(); | |
for (Map.Entry<String, JsonElement> entry : object.entrySet()) { | |
// System.out.println(entry.getKey() + " " + entry.getValue()); | |
// Use default deserialisation for City objects: | |
City city = context.deserialize(entry.getValue(), City.class); | |
cities.add(city); | |
} | |
return new CityList(cities); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment