简体   繁体   English

如何检查 JSON 数组的 object 是否包含 Android Java 中具有特定名称的数字?

[英]How can I check if object of JSON array contains number with certain name in Android Java?

I have this JSON array and I need to check every object of it if it has a "rain" number as 1st object does.我有这个 JSON 数组,我需要检查它的每个 object 是否像第一个 object 那样有一个“雨”数。 How can I implement it in Java, Android?如何在Java、Android中实现? I am making an android app that needs to analyze if it rains someday.我正在制作一个 android 应用程序,它需要分析某天是否下雨。

 "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
        }

You would like to check if "rain" attribute is returned in the response.您想要检查响应中是否返回了"rain"属性。

To do so, follow these steps:为此,请按照下列步骤操作:

  1. Create a class for the object in the JSON array.为JSON数组中的object创建一个class。
  2. Use Gson or any other JSON library in Android to convert the fetched JSON into the created class object.使用 Gson 或 Android 中的任何其他 JSON 库将获取的 JSON 转换为创建的 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.然后检查 rain 是否不是 null 并且 rain 是否不等于 0 以确认 rain 属性存在于数组中的 JSON 对象中。
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
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查2D对象数组是否包含某个对象? (Java) - How to check if a 2D object array contains a certain object? (Java) 如何检查文件中是否包含用户输入的特定数字? (Java)的 - How can I check if a file contains a certain number that the user has input? (java) 如何检查数组是否包含某个对象? - How to check if an array contains a certain object? 如何在 Json Object 中解析多个具有相同名称的 Json 数组 - How can I parse a number of Json Array with same name in a Json Object 如何使用Java在特定索引中检查2D数组中的哪个对象? - How can I check which object is in my 2D Array in a certain index with Java? 如何在没有循环键的情况下检查键名是否包含嵌套 json 对象的数字/特殊字符? - How to check if a key name contains a number/special character for a nested json object without looping keys? 我可以检查 List 是否包含一个对象,该对象具有在 java 中具有特定值的字段吗? - Can I check whether a List contains an object that has fields with a certain value in java? 如果此 object 包含具有特定值的密钥,如何在数组中删除 JSON 和 Object? - How to remove in a JSON an Object in an Array if this object contains a key with certain value? 如何检查字符串是否包含(数字) - How can I check if a string contains (number) Java如何检查数组列表中的对象是否包含某个值 - Java how to check if an object in an arraylist contains a certain value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM