英文:
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"
属性。
要实现这个目标,请按照以下步骤进行操作:
- 为JSON数组中的对象创建一个类。
- 在Android中使用Gson或任何其他JSON库,将获取到的JSON转换为创建的类对象。
- 然后检查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:
- Create a class for the object in the JSON array.
- Use Gson or any other JSON library in Android to convert the fetched JSON into the created class object.
- 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 < c.length(); i++) {
JSONObject obj = c.getJSONObject(i);
if(obj.has("rain")){
//object has "rain" property
}
else{
//object doesnt have "rain" property
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论