所以我试图在JAVA控制台上读取和打印json文件作为字符串。程序不是打印json内容,而是打印引用类的默认属性值。我该如何解决这个问题?我的代码是这样的
public class test {
private static final String WEATHER_URL = "https://api.collectapi.com/weather/getWeather?data.lang=tr&data.city=istanbul";
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = (HttpRequest) HttpRequest.newBuilder()
.GET()
.header("content-type", "application/json")
.header("authorization", "apikey myapikey")
.uri(URI.create(WEATHER_URL))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//parse JSON into objects
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setVisibility(VisibilityChecker.Std.defaultInstance().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
List<Data>data = mapper.readValue(response.body(), new TypeReference<List<Data>>() { });
System.out.println(data);
//CONSOLE OUTPUT
[Data{date=null, day=null, icon=null, description=null, status=null, degree=0.0, min=0.0, max=0.0, night=0.0, humidity=0}]
------------------------------------------------------------------------
BUILD SUCCESS
//数据类public class Data {
private String date;
private String day ;
private String icon ;
private String description;
private String status ;
private double degree;
private double min;
private double max ;
private double night ;
private int humidity;
//这里有getter和setter
@Override
public String toString() {
return "Data{" + "date=" + date + ", day=" + day + ", icon=" + icon + ", description=" + description + ", status=" + status + ", degree=" + degree + ", min=" + min + ", max=" + max + ", night=" + night + ", humidity=" + humidity + '}';
}
//JSON文件结构是这样的
{
"result": [
{
"date": "24.09.2018",
"day": "Pazartesi",
"icon": "https://image.flaticon.com/icons/svg/143/143769.svg",
"description": "açık",
"status": "Clear",
"degree": "31",
"min": "11.6",
"max": "31",
"night": "11.6",
"humidity": "17"
},
{
"date": "25.09.2018",
"day": "Salı",
"icon": "https://image.flaticon.com/icons/svg/143/143769.svg",
"description": "yağmurlu",
"status": "Rainy",
"degree": "24.14",
"min": "7.63",
"max": "25.82",
"night": "9.09",
"humidity": "35"
},
"..."
]
}
将Apache常用工具添加到pom中,它将像charm一样工作
import org.apache.commons.io.IOUtils;
String theJsonString = "";
theJsonString=IOUtils.toString(classLoader.getResourceAsStream("/path/file.json"));
您解析响应的方式对我来说似乎是错误的。JSON文件结构响应不是以JSONArray开始的,实际的JSON响应是一个对象,它有一个名为result
的键,这是一个JSONObject
的数组,所以下面这行在这里不起作用。
List<Data>data = mapper.readValue(response.body(), new TypeReference<List<Data>>() { });
response.body()
不是返回一个数组,它是一个对象,所以如果你想从响应中得到数组,你需要首先得到result
,然后你可以解析数组。
您可能需要先更新POJO:
public class Response {
private Data result;
}
class Data {
private String date;
private String day ;
private String icon ;
private String description;
private String status ;
private double degree;
private double min;
private double max ;
private double night ;
private int humidity;
}
然后像这样可以解析:
final Response response = mapper.readValue(response.body(), new TypeReference<Response>() { });
final List<Data> data = response.result