[英]Convert JSON from URL to JSONNode
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; 线程“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) 位于sun.reflect.NativeMethodAccessorImpl.invoke的sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)的WeatherClient.main(WeatherClient.java:10)中的com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:1792) (NativeMethodAccessorImpl.Iava: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
: 当杰克逊拥有全能的JsonNode
时Map<String, Object>
我认为你没有理由需要使用Map<String, Object>
:
final JsonNode node = new ObjectMapper().readTree(new URL("yourURLHere");
See the javadoc for JsonNode
. 请参阅javadoc以获取JsonNode
。 You can navigate your JSON easily, in fact much more easily than with a Map
. 您可以轻松导航JSON,实际上比使用Map
更容易。 See methods .get()
, .path()
, etc etc. 请参阅方法.get()
.path()
等。
(shameless plug) And if you want to use JSON Pointer to navigate your JSON, you can also do that : (无耻的插件)如果你想使用JSON指针来导航你的JSON, 你也可以这样做 :
final JsonPointer ptr = JsonPointer.of("current_observation", "display_location");
final JsonNode displayLocation = ptr.get(node);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.