我的代码现在是:
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();
}
}
}
我得到:
线程“main”中的异常 java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.createParser(Ljava/net/URL;)Lcom/fasterxml/jackson/core/JsonParser; 在 com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:1792) 在 WeatherClient.main(WeatherClient.java:10) 在 sun.reflect.NativeMethodAccessorImpl.invoke0( native 方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:601) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
有什么办法可以解决吗?
最佳答案
我看不出你为什么需要使用 Map<String, Object>
当 jackson 拥有全能JsonNode
:
final JsonNode node = new ObjectMapper().readTree(new URL("yourURLHere");
参见 javadoc for JsonNode
.您可以轻松地浏览您的 JSON,实际上比使用 Map
更容易.查看方法 .get()
, .path()
等等等等
(无耻的插件)如果你想使用JSON Pointer导航您的 JSON,you can also do that :
final JsonPointer ptr = JsonPointer.of("current_observation", "display_location");
final JsonNode displayLocation = ptr.get(node);
关于java - 将 JSON 从 URL 转换为 JSONNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17629121/