zahra zahra - 1 month ago 16
Java Question

jackson.annotation import cannot resolve

I am trying to write a program that parse JSON in java. Two of the imports that I am using could not be resolved:
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.annotation.JsonMappingException;

I managed to resolve the jackson.core by downloading the version 2.2.3 and adding it to jars in the build path, But I still have the same error for jackson.annotation even after I downloaded jackson-annotations-2.3.3.jar(The latest version as I know) and added it to jars. I would need it to work so I can make my JsonMappingException work.
This is part of the code I have:

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]);
}
}

}


Any ideas?

Answer

After some researches on the web I found out that I do not need import com.fasterxml.jackson.annotation.JsonMappingException; for my JsonMappingException to work. but I need import com.fasterxml.jackson.databind.JsonMappingException; for it. So my code is like this after importing the 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());
        }
    }

}