可能将 JSONObject 转换为 java 字符串
Possibly converting JSONObject to java string
[澄清我正在处理]
我能看到的所有问题都是关于反过来做的。我有一个变量,它是从天气 API 中获取的字符串,我想测试它是否是某个单词(即 "Rain" 或 "Clouds")。我已经为这段代码设置了框架,但它似乎不喜欢我检查上面解释的部分。我认为这可能是因为我试图将 JSONObject 与普通 java 字符串进行比较,但在我看过的所有地方中,我没有得到关于如何转换它的直接答案。有人可以帮我吗?
我不太擅长编码,如果我有点慢,请见谅。
这里是创建 JSONObject 的地方
JSONArray cityWeatherObject = city.getJSONArray("weather");
JSONObject weather = cityWeatherObject.getJSONObject(0);
cityDesc = weather.getString("main");
这是我尝试将它与字符串进行比较的地方
void drawWeather() {
println(cityDesc, "Clouds");
if (cityDesc == "Rain") {
for (int i = 0; i < rainDrops.length; i++) {
rainDrops[i].wind = cityWind;
rainDrops[i].fall();
rainDrops[i].spawn();
}
}
if (cityDesc == "Clouds") {
println("Clouds selected");
for (int i = 0; i < clouds.length; i++) {
clouds[i].wind = cityWind;
clouds[i].drift();
clouds[i].spawn();
}
}
}
如果我需要提供更多说明,请告诉我。
To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)
来源:here
[澄清我正在处理]
我能看到的所有问题都是关于反过来做的。我有一个变量,它是从天气 API 中获取的字符串,我想测试它是否是某个单词(即 "Rain" 或 "Clouds")。我已经为这段代码设置了框架,但它似乎不喜欢我检查上面解释的部分。我认为这可能是因为我试图将 JSONObject 与普通 java 字符串进行比较,但在我看过的所有地方中,我没有得到关于如何转换它的直接答案。有人可以帮我吗?
我不太擅长编码,如果我有点慢,请见谅。
这里是创建 JSONObject 的地方
JSONArray cityWeatherObject = city.getJSONArray("weather");
JSONObject weather = cityWeatherObject.getJSONObject(0);
cityDesc = weather.getString("main");
这是我尝试将它与字符串进行比较的地方
void drawWeather() {
println(cityDesc, "Clouds");
if (cityDesc == "Rain") {
for (int i = 0; i < rainDrops.length; i++) {
rainDrops[i].wind = cityWind;
rainDrops[i].fall();
rainDrops[i].spawn();
}
}
if (cityDesc == "Clouds") {
println("Clouds selected");
for (int i = 0; i < clouds.length; i++) {
clouds[i].wind = cityWind;
clouds[i].drift();
clouds[i].spawn();
}
}
}
如果我需要提供更多说明,请告诉我。
To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)
来源:here