Home > database >  Extracting JSON Objects sharing the same name from API Call JSON response into an array using Java
Extracting JSON Objects sharing the same name from API Call JSON response into an array using Java

Time:01-04

I am making weather forecast app using Android Studio, Java and OpenWeatherAPI. I got stuck on a problem and can't find solution. It is a JSON response I get from API:

{
  "cod": "200",
  "message": 0,
  "cnt": 40,
  "list": [
    {
      "dt": 1672812000,
      "main": {
        "temp": 277.75,
        "feels_like": 273.5,
        "temp_min": 277.75,
        "temp_max": 279.26,
        "pressure": 1014,
        "sea_level": 1014,
        "grnd_level": 1015,
        "humidity": 85,
        "temp_kf": -1.51
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03n"
        }
      ],
      "clouds": {
        "all": 29
      },
      "wind": {
        "speed": 6.1,
        "deg": 197,
        "gust": 15.31
      },
      "visibility": 10000,
      "pop": 0,
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2023-01-04 06:00:00"
    },
    
    {
      "dt": 1672844400,
      "main": {
        "temp": 282.13,
        "feels_like": 278.47,
        "temp_min": 282.13,
        "temp_max": 282.13,
        "pressure": 1012,
        "sea_level": 1012,
        "grnd_level": 1007,
        "humidity": 84,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 100
      },
      "wind": {
        "speed": 8.08,
        "deg": 234,
        "gust": 18.26
      },
      "visibility": 10000,
      "pop": 1,
      "rain": {
        "3h": 2.84
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2023-01-04 15:00:00"
    },
    {
      "dt": 1673233200,
      "main": {
        "temp": 280.69,
        "feels_like": 277.6,
        "temp_min": 280.69,
        "temp_max": 280.69,
        "pressure": 998,
        "sea_level": 998,
        "grnd_level": 993,
        "humidity": 72,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 88
      },
      "wind": {
        "speed": 5.14,
        "deg": 207,
        "gust": 12.85
      },
      "visibility": 10000,
      "pop": 0,
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2023-01-09 03:00:00"
    }
  ],
  "city": {
    "id": 2950159,
    "name": "Berlin",
    "coord": {
      "lat": 52.5244,
      "lon": 13.4105
    },
    "country": "DE",
    "population": 1000000,
    "timezone": 3600,
    "sunrise": 1672816596,
    "sunset": 1672844715
  }
}

And my question is: Is there a way to extract all keys for example "weather"

    "weather": [ 
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],

into and array (of JSONObjects?) so the information can be accessed like this or in similiar fashion:

weather_arrray[i].getString("description")

I have been searching online for solution using libraries like Gson or Jackson but I didn't find any clues how to get it done. I am opened to any other ideas if putting it in arrays is not correct approach. I just need to be able to easily access info from for example 1st, 4th or 27th etc. "weather" or lets say "main" occurence.

CodePudding user response:

you can do this using dynamic JSON Parsor.

If your response contains JSONObject, then store it as a JSONObject, OR if your response contains JSONArray, then store it as a JSONArray.

Pass your response as an argument inside below function, and check that.

Here Iterator is used to fetch dynamic key names from JSONObject.

private void parseJson(JSONObject data) {

        if (data != null) {
            Iterator<String> it = data.keys();
            while (it.hasNext()) {
                String key = it.next();
                try {
                    if (data.get(key) instanceof JSONArray) {
                        JSONArray arry = data.getJSONArray(key);
                        int size = arry.length();
                        for (int i = 0; i < size; i  ) {
                            parseJson(arry.getJSONObject(i));
                        }
                    } else if (data.get(key) instanceof JSONObject) {
                        parseJson(data.getJSONObject(key));
                    } else {
                        System.out.println(key   ":"   data.getString(key));
                    }
                } catch (Throwable e) {
                    try {
                        System.out.println(key   ":"   data.getString(key));
                    } catch (Exception ee) {
                    }
                    e.printStackTrace();

                }
            }
        }
    } 
  • Related