如何在 Android 的 Java 中检查 JSON 数组中的对象是否包含特定名称的数字?

huangapple go评论91阅读模式
英文:

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

问题

import org.json.JSONArray;
import org.json.JSONObject;

public class RainChecker {

    public static void main(String[] args) {
        String jsonString = "{\n" +
                "    \"daily\": [\n" +
                "        {\n" +
                "            \"dt\": 1597730400,\n" +
                "            \"sunrise\": 1597705822,\n" +
                "            \"sunset\": 1597758442,\n" +
                "            \"temp\": {\n" +
                "                \"day\": 299.15,\n" +
                "                \"min\": 288.84,\n" +
                "                \"max\": 299.15,\n" +
                "                \"night\": 288.84,\n" +
                "                \"eve\": 298.91,\n" +
                "                \"morn\": 299.15\n" +
                "            },\n" +
                "            \"feels_like\": {\n" +
                "                \"day\": 291.47,\n" +
                "                \"night\": 287.34,\n" +
                "                \"eve\": 293.53,\n" +
                "                \"morn\": 291.47\n" +
                "            },\n" +
                "            \"pressure\": 997,\n" +
                "            \"humidity\": 27,\n" +
                "            \"dew_point\": 278.72,\n" +
                "            \"wind_speed\": 9.52,\n" +
                "            \"wind_deg\": 200,\n" +
                "            \"weather\": [\n" +
                "                {\n" +
                "                    \"id\": 500,\n" +
                "                    \"main\": \"Rain\",\n" +
                "                    \"description\": \"light rain\",\n" +
                "                    \"icon\": \"10d\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"clouds\": 100,\n" +
                "            \"pop\": 0.86,\n" +
                "            \"rain\": 1.03,\n" +
                "            \"uvi\": 4.86\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        JSONObject jsonObject = new JSONObject(jsonString);
        JSONArray dailyArray = jsonObject.getJSONArray("daily");
        JSONObject firstObject = dailyArray.getJSONObject(0);
        double rainValue = firstObject.getDouble("rain");

        if (rainValue == 1.0) {
            System.out.println("The first object has a rain value of 1.0.");
            // Perform your analysis here
        } else {
            System.out.println("The first object does not have a rain value of 1.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
}

答案1

得分: 1

你想检查响应中是否返回了 "rain" 属性。

要实现这个目标,请按照以下步骤进行操作:

  1. 为JSON数组中的对象创建一个类。
  2. 在Android中使用Gson或任何其他JSON库,将获取到的JSON转换为创建的类对象。
  3. 然后检查rain是否不为null,并且rain不等于0,以确认JSON数组中的对象中存在rain属性。
英文:

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.

答案2

得分: 1

String yourJSONString; // 应该保存你拥有的JSON数据
JSONArray c = JSONArray(yourJSONString);
for (int i = 0 ; i < c.length(); i++) {
    JSONObject obj = c.getJSONObject(i);
    if(obj.has("rain")){
        // 对象具有"rain"属性
    }
    else{
        // 对象不具有"rain"属性
    }
}
英文:
String yourJSONString; //should hold the JSON you have 
JSONArray c = JSONArray(yourJSONString);
for (int i = 0 ; i &lt; c.length(); i++) {
JSONObject obj = c.getJSONObject(i);
if(obj.has(&quot;rain&quot;)){
//object has &quot;rain&quot; property
}
else{
//object doesnt have &quot;rain&quot; property
}
}

huangapple
  • 本文由 发表于 2020年8月18日 17:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63465497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定