Home > front end >  How do i access an array in openweathermap json using java?
How do i access an array in openweathermap json using java?

Time:10-28

enter image description here

In the JSON -photo, I have highlighted the part I need to access in my code and display it in my app.

I tried using JSONArray array = response.getJSONArray("weather[0]"), but that led me literally no where haha

So far i have nothing on the matter. Also here is my code.

public void getWeather(View view) {
    String apiKey="40bb658a709ebb7b1c210655e7f5cfe6";
    String city=et.getText().toString();
    String url="https://api.openweathermap.org/data/2.5/weather?q=" city "&appid=40bb658a709ebb7b1c210655e7f5cfe6&units=metric";
    RequestQueue queue= Volley.newRequestQueue(getApplicationContext());
    JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONObject object = response.getJSONObject("sys");
                String country = object.getString("country");
                tvcountry.setText(country);

                JSONObject object1= response.getJSONObject("main");
                String temperature=object1.getString("temp");
                tvTemp.setText("Current: "   temperature   " C");

                JSONObject object2 = response.getJSONObject("main");
                String feelsLike = object2.getString("feels_like");
                tvFeels_Like.setText("Feels like: "   feelsLike   " C");

                JSONObject object3 = response.getJSONObject("main");
                String maxtemp = object3.getString("temp_max");
                tvMaxTemp.setText("Max: "   maxtemp   " C");

                JSONObject object4 = response.getJSONObject("main");
                String mintemp = object4.getString("temp_min");
                tvMinTemp.setText("Min: "   mintemp   " C");
                
                JSO



            } catch (JSONException e) {
                Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void one rrorResponse(VolleyError error) {
            Toast.makeText(SecondPage.this, error.toString(), Toast.LENGTH_SHORT).show();

        }
    });
    queue.add(request);
}

Thank you in advance

CodePudding user response:

Use a JSON parser. I am referring to Genson (see http://genson.io/GettingStarted/#deserialize-generic-types):

Genson genson = new Genson();
List<Object> persons = genson.deserialize("[{\"age\":28,\"name\":\"Foo\"}]", List.class);

CodePudding user response:

You can continue with org.json, too:

    @Test
    void getFirstIndexFromJsonArray() {
        // Given
        String rawJson = "{\n"  
                "  \"coors\": {\n"  
                "    \"long\": -0.7888,\n"  
                "    \"lat\": 51.9000\n"  
                "  },\n"  
                "  \"weather\": [\n"  
                "    {\n"  
                "      \"id\": 804,\n"  
                "      \"main\": \"Clouds\",\n"  
                "      \"description\": \"overcast clouds\",\n"  
                "      \"icon\": \"04n\"\n"  
                "    },\n"  
                "    {\n"  
                "      \"id\": 1044,\n"  
                "      \"main\": \"Thunder\",\n"  
                "      \"description\": \"Thunderbird\",\n"  
                "      \"icon\": \"1944x\"\n"  
                "    }\n"  
                "  ]\n"  
                "}";

        // When
        JSONObject jsonObject = new JSONObject(rawJson);
        JSONArray weather = jsonObject.getJSONArray("weather");
        JSONObject firstWeatherEntry = new JSONObject(weather.get(0).toString());

        // Then
        assertThat(firstWeatherEntry.get("description"), is("overcast clouds"));
    }
  • Related