Tell me how to properly use GSON, or where you can find information about its use?
I want to take the WeatherOpenApi
data and create a new Weather
object, filling its variables name
, main
, maxtemp
received data.
By what actions should the data that I get in the form of json should be distributed among the class variables? As an example, I looked here , but there the number of object variables is equal to the number of arguments in json — and in my case there is a lot of superfluous information — how to weed out the extra?
public class MainActivity extends AppCompatActivity { public class Weather { String main; String name; double maxtemp; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String jsonlink = "{\"coord\":{\"lon\":-0.13,\"lat\":51.51},\"weather\":[{\"id\":310,\"main\":\"Drizzle\",\"description\":\"light intensity drizzle rain\",\"icon\":\"09d\"},{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":280.87,\"pressure\":1007,\"humidity\":87,\"temp_min\":280.15,\"temp_max\":281.75},\"wind\":{\"speed\":4.6,\"deg\":230},\"clouds\":{\"all\":90},\"dt\":1459925764,\"sys\":{\"type\":1,\"id\":5168,\"message\":0.0045,\"country\":\"GB\",\"sunrise\":1459920191,\"sunset\":1459968198},\"id\":2643743,\"name\":\"London\",\"cod\":200}"; GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); Weather weather = gson.fromJson(jsonlink, Weather.class); Log.i("Name", weather.name); } }
I would be grateful for the help!