0

I have this JSON array and I need to check every object of it if it has a "rain" number as 1st object does. How can I implement it in Java, Android? I am making an android app that needs to analyze if it rains someday.

 "daily": [
        {
            "dt": 1597730400,
            "sunrise": 1597705822,
            "sunset": 1597758442,
            "temp": {
                "day": 299.15,
                "min": 288.84,
                "max": 299.15,
                "night": 288.84,
                "eve": 298.91,
                "morn": 299.15
            },
            "feels_like": {
                "day": 291.47,
                "night": 287.34,
                "eve": 293.53,
                "morn": 291.47
            },
            "pressure": 997,
            "humidity": 27,
            "dew_point": 278.72,
            "wind_speed": 9.52,
            "wind_deg": 200,
            "weather": [
                {
                    "id": 500,
                    "main": "Rain",
                    "description": "light rain",
                    "icon": "10d"
                }
            ],
            "clouds": 100,
            "pop": 0.86,
            "rain": 1.03,
            "uvi": 4.86
        }
2
  • 2
    Please update the question with a valid json and format it. Also, do show us the code you have tried so far. Commented Aug 18, 2020 at 9:19
  • Updated the post. Commented Aug 18, 2020 at 9:24

2 Answers 2

1
String yourJSONString; //should hold the JSON you have 
JSONArray c = JSONArray(yourJSONString);
    for (int i = 0 ; i < c.length(); i++) {
        JSONObject obj = c.getJSONObject(i);
        if(obj.has("rain")){
          //object has "rain" property
        }
        else{
          //object doesnt have "rain" property
        }
    }

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help, I cnhanged your code a little but I got the problem. The length of an array is 8 but LogCat shows only 2 messages. What I made wrong? See the screenshot imgur.com/a/QwM3Xfp
This is very weird. Check if you're getting an exception.
Also, post your entire JSON array, are you sure its length is 8?
I figured it out. Log cat shows 2020-08-19 16:59:29.653 10012-10012/com.example.carweather I/MyResult: Doesn't rain 2020-08-19 16:59:29.653 10012-10012/com.example.carweather I/chatty: uid=10305(com.example.carweather) identical 6 lines 2020-08-19 16:59:29.653 10012-10012/com.example.carweather I/MyResult: Doesn't rain. The problem is "identical 6 lines". The compiler just makes all the "Doesn't rain to this.
And here is the result for my town with more rainy weather imgur.com/a/Zk2OO7C thank you again!
|
1

You would like to check if "rain" attribute is returned in the response.

To do so, follow these steps:

  1. Create a class for the object in the JSON array.
  2. Use Gson or any other JSON library in Android to convert the fetched JSON into the created class object.
  3. Then check if rain is not null and if rain is not equal to 0 to confirm rain attribute is present in the JSON objects in the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.