My code is now:
import java.net.URL;
import java.util.HashMap;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.*;
public class WeatherClient {
public static void main (String[] args) {
try { final JsonNode node = new ObjectMapper().readTree(new URL("http://api.wunderground.com/api/5bef7a0ecc7f2933/" +
"conditions/q/CA/San_Francisco.json"));
}
catch (Exception exception) {
exception.printStackTrace();
}
}
}
and I get:
Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.createParser(Ljava/net/URL;)Lcom/fasterxml/jackson/core/JsonParser; at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:1792) at WeatherClient.main(WeatherClient.java:10) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Is there any way to solve this?
I see no reason why you'd need to use a Map<String, Object> when Jackson has the almighty JsonNode:
final JsonNode node = new ObjectMapper().readTree(new URL("yourURLHere");
See the javadoc for JsonNode. You can navigate your JSON easily, in fact much more easily than with a Map. See methods .get(), .path(), etc etc.
(shameless plug) And if you want to use JSON Pointer to navigate your JSON, you can also do that:
final JsonPointer ptr = JsonPointer.of("current_observation", "display_location");
final JsonNode displayLocation = ptr.get(node);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With