2
votes

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?

1
Hmmwait, what do you want? To obtain the JSON at that URI as a Map? Why not as a JsonNode?fge
@fge I wanted to do it as a Map so I could then categorise it, i.e. I don't need the whole thing. Hope that makes sense.Michael Nares
Well, see my answer then. JsonNode is much more suited for JSON navigation than a Map.fge
@fge Yeah cheers for that, sorry I didn't see your answer before commenting, otherwise I wouldn't have done so. Thanks again.Michael Nares

1 Answers

5
votes

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