簡體   English   中英

將JSON從URL轉換為JSONNode

[英]Convert JSON from URL to JSONNode

我的代碼現在是:

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

有什么方法可以解決這個問題嗎?

當傑克遜擁有全能的JsonNodeMap<String, Object>我認為你沒有理由需要使用Map<String, Object>

final JsonNode node = new ObjectMapper().readTree(new URL("yourURLHere");

請參閱javadoc以獲取JsonNode 您可以輕松導航JSON,實際上比使用Map更容易。 請參閱方法.get() .path()等。

(無恥的插件)如果你想使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM