1

Context: I'm having trouble getting a value out of a JSON returned by OpenWeatherMap's API via Android.

My JSON looks like this:

{"coord":{"lon":-78.32,"lat":38.55},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"base":"stations","main":{"temp":269.05,"feels_like":265.34,"temp_min":267.59,"temp_max":270.37,"pressure":1010,"humidity":78},"visibility":10000,"wind":{"speed":1.25,"deg":293},"clouds":{"all":25},"dt":1607493640,"sys":{"type":3,"id":2006561,"country":"US","sunrise":1607516385,"sunset":1607550737},"timezone":-18000,"id":4744896,"name":"Ashbys Corner","cod":200}

It is stored in a JsonObject (Part of GSON, not to be confused with JSONObject) from a URL like so:

URLConnection requestWeather = weatherUrl.openConnection();
requestWeather.connect(); // connect to the recently opened connection to the OpenWeatherMaps URL
JsonElement parsedJSON = JsonParser.parseReader(new InputStreamReader((InputStream) requestWeather.getContent())); //Convert the input stream of the URL into JSON
JsonObject fetchedJSON = parsedJSON.getAsJsonObject(); // Store the result of the parsed json locally as a json object

Problem: I want to get the value associated with "main" out of the JSON (which in this case should be "Clouds").

Attempted Solution: I've attempted to get the value of main out like this:

String weatherType = fetchedJSON.get("weather").getAsString();

but this throws a java.lang.UnsupportedOperationException: JsonObject exception.

Question: How do I get the value of "main"?

Prithvi Boinpally
  • 427
  • 1
  • 9
  • 23
  • `main` is a `JsonObject` itself first you get main with `JsonObject main =fetchedJSON.getAsJsonObject("main")` then you can get inner properties of main . If you using Gson why are you still doing parsing manually ? – ADM Dec 09 '20 at 06:56
  • Hmm can I call "getAsJsonObject" on "main" if "main" is nested within "weather"? – Prithvi Boinpally Dec 09 '20 at 07:13
  • To be completely honest I find the answer to the question you linked a bit confusing. – Prithvi Boinpally Dec 09 '20 at 07:13
  • I find it far more confusing, that this question doesn't have the least to do with GSON. – Martin Zeitler Dec 09 '20 at 07:35
  • It's using GSON methods/objects like "JsonElement, JsonObject, JsonParser" etc. And one of GSON's various get methods I suspect is the solution to my problem. – Prithvi Boinpally Dec 09 '20 at 07:36

1 Answers1

1

You can use JACKSON or GSON library for fast parsing of Json data using model class. JACKSON and GSON are dedicated to processing (serializing/deserializing) JSON data.

Raw Parsing via GSON

JsonObject fetchedJSON = parsedJSON.getAsJsonObject();
//weather is an array so get it as array not as string
JsonArray jarray = fetchedJSON.getAsJsonArray("weather");
// OR you use loop if you want all main data
jobject = jarray.get(0).getAsJsonObject();
String main= jobject.get("main").getAsString();

Although if you want raw parsing then you can do like ::

JSONObject obj = new JSONObject(yourString);

JSONArray weather = obj.getJSONArray("weather");
for (int i = 0; i < arr.length(); i++)
{
String main= arr.getJSONObject(i).getString("main");
......
}
Vishal Chauhan
  • 932
  • 1
  • 6
  • 11