我正在尝试编写一个在java中解析JSON的程序。我正在使用的两个导入无法解决: import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.annotation.JsonMappingException;
我设法通过下载版本2.2.3并将其添加到构建路径中的jar中来解析jackson.core但是即使在我下载了jackson-annotations-2.3.3之后,我仍然对jackson.annotation有相同的错误。 .jar(我知道的最新版本)并将其添加到罐子里。我需要它才能工作,所以我可以使我的JsonMappingException工作。 这是我的代码的一部分:
package weather.data;
//java object imports
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.annotation.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.net.URL;
import java.io.IOException;
public class usertest {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException
{
URL jsonUrl = new URL("http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139");
ObjectMapper mapper = new ObjectMapper();
Jweather jweather = mapper.readValue(jsonUrl, Jweather.class);
for (int i=0; i<10; i++){
System.out.println(temp[i]);
}
}
}
有什么想法吗?
答案 0 :(得分:0)
在对网络进行一些研究后,我发现我不需要 import com.fasterxml.jackson.annotation.JsonMappingException; 为我的JsonMappingException工作。但是我需要 import com.fasterxml.jackson.databind.JsonMappingException; 为了它。因此,在导入databind.JsonMappingException
之后,我的代码就像这样package weather.data;
//java object imports
import com.fasterxml.jackson.core.JsonParseException;
//import com.fasterxml.jackson.annotation.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonMappingException;
import java.io.File;
import java.net.URL;
import java.io.IOException;
public class usertest {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException
{
URL jsonUrl = new URL("http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139");
ObjectMapper mapper = new ObjectMapper();
Jweather jweather = mapper.readValue(jsonUrl, Jweather.class);
for (int i=0; i<10; i++){
System.out.println(jweather.getList().get(i).getMain().getTemp());
}
}
}