gpt4 book ai didi

java - 没有来自 JSON 的值

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:12 25 4
gpt4 key购买 nike

我想显示 JSON 中的值只是为了测试目的,但我实际上什么也没收到。哪里可能有问题? Utils 中的链接肯定是正确的,我已经在浏览器上运行了它,一切都很好。

这是代码

实用程序类

public class WeatherUtils {
public WeatherUtils(){}

public static ArrayList<Weather> getHourlyData (double minTemp, double maxTemp, double currentTemp, double airPressure){

ArrayList<Weather> weatherList = new ArrayList<>();

try {
JSONObject reader = new JSONObject("https://api.openweathermap.org/data/2.5/forecast?q=London,us&units=metric&appid=ID...");
JSONArray array = reader.getJSONArray("list");
for (int i = 0; i<array.length(); i++){

JSONObject secondReader = array.getJSONObject(i);
JSONObject dataObject = secondReader.getJSONObject("main");
for (int j = 0; j<dataObject.length(); j++){
currentTemp = dataObject.getDouble("temp");
minTemp = dataObject.getDouble("temp_min");
maxTemp = dataObject.getDouble("temp_max");
airPressure = dataObject.getDouble("pressure");
}
weatherList.add(new Weather(currentTemp,minTemp,maxTemp,airPressure));
}

} catch (JSONException e) {
e.printStackTrace();
}
return weatherList;
}
}

主要 Activity 双 a、b、c、d;

    a = 0.0;
b = 0.0;
c = 0.0;
d = 0.0;

ArrayList<Weather> weathers = WeatherUtils.getHourlyData(a,b,c,d);
System.out.println(weathers);

JSON 结构

 {
"cod": "200",
"message": 0.0074,
"cnt": 40,
"list": [
{
"dt": 1559131200,
"main": {
"temp": 22.1,
"temp_min": 21.32,
"temp_max": 22.1,
"pressure": 1012.31,
"sea_level": 1012.31,
"grnd_level": 976.84,
"humidity": 92,
"temp_kf": 0.78
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": {
"all": 89
},
"wind": {
"speed": 3.08,
"deg": 213.025
},
"rain": {
"3h": 0.875
},
"sys": {
"pod": "d"
},
"dt_txt": "2019-05-29 12:00:00"
},
{

当然,还有更多数据。我发布了一个“区 block ”

我该如何解决这个问题?

最佳答案

好吧,考虑到您只是想“测试”json 解析,您几乎没有什么选择,但让我们选择一个简单的。但首先,我想说提取解析器并将其放入自己的类/方法中,以便更容易测试,如下所示:

public class WeatherUtils {
public WeatherUtils(){}

public static ArrayList<Weather> getHourlyData (double minTemp, double maxTemp, double currentTemp, double airPressure){

final ArrayList<Weather> weatherList = new ArrayList<>();

try {
final JSONObject response = httpCall();
weatherList = mapWeatherResponse(response);
} catch (JSONException e) {
e.printStackTrace();
}
return weatherList;
}

public static List<Weather> mapWeatherResponse(JSONObject reader){
final ArrayList<Weather> weatherList = new ArrayList<>();
JSONArray array = reader.getJSONArray("list");
for (int i = 0; i<array.length(); i++){
JSONObject secondReader = array.getJSONObject(i);
JSONObject dataObject = secondReader.getJSONObject("main");
for (int j = 0; j<dataObject.length(); j++){
currentTemp = dataObject.getDouble("temp");
minTemp = dataObject.getDouble("temp_min");
maxTemp = dataObject.getDouble("temp_max");
airPressure = dataObject.getDouble("pressure");
}
weatherList.add(new Weather(currentTemp,minTemp,maxTemp,airPressure));
}
}
}

使用 junit 测试测试响应解析器:

您可以像这样创建 junit 测试:

public class WeatherUtilsTest {
@Test
public void parserResponseTEst() {
final List<String> expectedResponse = new ArrayList<>();
//fill the expectedResponse with the correspondent values

final String json = "{\n" +
" \"cod\": \"200\",\n" +
" \"message\": 0.0074,\n" +
" \"cnt\": 40,\n" +
" \"list\": [\n" +
" {\n" +
" \"dt\": 1559131200,\n" +
" \"main\": {\n" +
" \"temp\": 22.1,\n" +
" \"temp_min\": 21.32,\n" +
" \"temp_max\": 22.1,\n" +
" \"pressure\": 1012.31,\n" +
" \"sea_level\": 1012.31,\n" +
" \"grnd_level\": 976.84,\n" +
" \"humidity\": 92,\n" +
" \"temp_kf\": 0.78\n" +
" },\n" +
" \"weather\": [\n" +
" {\n" +
" \"id\": 500,\n" +
" \"main\": \"Rain\",\n" +
" \"description\": \"light rain\",\n" +
" \"icon\": \"10d\"\n" +
" }\n" +
" ],\n" +
" \"clouds\": {\n" +
" \"all\": 89\n" +
" },\n" +
" \"wind\": {\n" +
" \"speed\": 3.08,\n" +
" \"deg\": 213.025\n" +
" },\n" +
" \"rain\": {\n" +
" \"3h\": 0.875\n" +
" }\n" +
" }]\n" +
" }";

final List<String> response = WeatherUtils.mapWeatherResponse(new JSONObject(json));
assertEquals(expectedResponse, response);
}
}

您正在执行的 JSONObject 解析器没有任何问题。您提到您在Utils中使用的链接是正确的,当您在浏览器、 postman 、失眠中测试它时,您是否得到正确的响应?

OBS JSONObject reader = new JSONObject("https://api..."); 不会获取任何内容,您所做的是从给定的 String 创建一个 JSONObject,即“https://...”。要获取数据,您需要实现一些 http 客户端。这是一个例子https://stackoverflow.com/a/4457526/761668

关于java - 没有来自 JSON 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56361968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com