Skip to content

Instantly share code, notes, and snippets.

@jnizet
Created October 26, 2018 19:36
Show Gist options
  • Save jnizet/cc17ab01cec0bb4430b75a5cb482630f to your computer and use it in GitHub Desktop.
Save jnizet/cc17ab01cec0bb4430b75a5cb482630f to your computer and use it in GitHub Desktop.
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class A {
public static void main(String[] args) {
String jsonString = "{\n" +
" \"coord\": {\n" +
" \"lon\": -96.8,\n" +
" \"lat\": 32.78\n" +
" },\n" +
" \"weather\": [\n" +
" {\n" +
" \"id\": 801,\n" +
" \"main\": \"Clouds\",\n" +
" \"description\": \"few clouds\",\n" +
" \"icon\": \"02d\"\n" +
" }\n" +
" ],\n" +
" \"base\": \"stations\",\n" +
" \"main\": {\n" +
" \"temp\": 293.21,\n" +
" \"pressure\": 1015,\n" +
" \"humidity\": 52,\n" +
" \"temp_min\": 292.15,\n" +
" \"temp_max\": 294.15\n" +
" },\n" +
" \"visibility\": 16093,\n" +
" \"wind\": {\n" +
" \"speed\": 3.1,\n" +
" \"deg\": 340\n" +
" },\n" +
" \"clouds\": {\n" +
" \"all\": 20\n" +
" },\n" +
" \"dt\": 1540580100,\n" +
" \"sys\": {\n" +
" \"type\": 1,\n" +
" \"id\": 2592,\n" +
" \"message\": 0.0051,\n" +
" \"country\": \"US\",\n" +
" \"sunrise\": 1540557635,\n" +
" \"sunset\": 1540597262\n" +
" },\n" +
" \"id\": 4684888,\n" +
" \"name\": \"Dallas\",\n" +
" \"cod\": 200\n" +
"}";
System.out.println(getDescription(jsonString));
}
private static String getDescription(String jsonString) {
JsonObject currentWeather = new JsonParser().parse(jsonString).getAsJsonObject();
JsonArray weather = currentWeather.getAsJsonArray("weather");
JsonElement description = weather.get(0);
String weatherDescription = description.toString();
return weatherDescription;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment