【问题标题】:Import json URL to java and parse it using jackson library将json URL导入java并使用jackson库解析它
【发布时间】:2014-05-05 18:33:28
【问题描述】:

我正在尝试读取 java 中的 json 链接并对其进行解析,以便将其用于其他事务,但问题是我遇到了我真的不知道该怎么做的错误。 代码如下:

package weather.data;

import weather.data;

import com.fasterxml.jackson.core.JsonParseException;
//import com.fasterxml.jackson.annotation.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonMappingException;

import java.io.File;
import java.net.URI;
import java.io.IOException;

public class usertest {

    public static void main() throws JsonParseException, JsonMappingException, IOException 
    {

        URI jsonUrl = new URI("https://gist.githubusercontent.com/anonymous/4b32c7ef1ceb5dd48bf5/raw/ef1987551faa3fb61473bb0e7aad70a228dc36d6/gistfile1.txt");

        ObjectMapper mapper = new ObjectMapper();

        City = mapper.readValue(jsonUrl, data.class);
    }

}

以下是错误: 关于 import weather.data:这一行的多个标记 - 只能导入一个类型。 weather.data 解析为 包裹 - 导入的 weather.data 从未使用过

about City = mapper.readValue(jsonUrl, data.class):此行有多个标记 - 数据无法解析为类型 - 城市无法解析为变量 - ObjectMapper 类型中的方法 readValue(JsonParser, Class) 不适用于 参数(URI,类) - ObjectMapper 类型中的方法 readValue(JsonParser, Class) 不适用于 参数(URI,类) - 城市无法解析为变量 - 数据无法解析为类型

有什么想法吗?另外我真的不明白使用mapper.readValue的概念。有人能帮帮我吗?

注意:我使用 json gen 生成链接内的数据对象,现在我有对象数据 java 文件:City、Coord、List1、Temp 和 Weather,内容如下。

以 City.java 为例:

package weather.data;

import java.util.List;

public class City{
    private Coord coord;
    private String country;
    private Number id;
    private String name;
    private Number population;

    public Coord getCoord(){
        return this.coord;
    }
    public void setCoord(Coord coord){
        this.coord = coord;
    }
    public String getCountry(){
        return this.country;
    }
    public void setCountry(String country){
        this.country = country;
    }
    public Number getId(){
        return this.id;
    }
    public void setId(Number id){
        this.id = id;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public Number getPopulation(){
        return this.population;
    }
    public void setPopulation(Number population){
        this.population = population;
    }
}

提前致谢

【问题讨论】:

    标签: java json jackson


    【解决方案1】:

    你需要import weather.data.City(特定类)而不是import weather.data(一个包)。

    然后按照@jdiver 说的做:

    City city = mapper.readValue(jsonUrl, City.class);
    

    【讨论】:

    • 非常感谢@jcragun。我应用了你所说的关于导入的第一个错误消失了,但它仍然给我第二个代码的这个错误: 此行的多个标记 - ObjectMapper 类型中的方法 readValue(JsonParser, Class) 不适用于参数 (URI, Class) - ObjectMapper 类型中的方法 readValue(JsonParser, Class) 不适用于参数 (URI, Class)
    • 使用java.net.URL 代替URI 类。
    • 抱歉回答晚了。我的文件中已经有 java.net.URI。
    • UR L 而不是 I
    • 哦,愚蠢的错误。非常感谢。它奏效了。
    【解决方案2】:

    如果 URL 中的gistfile1.txt 文件包含 City 的 JSON 对象,则替换该行

    City = mapper.readValue(jsonUrl, data.class);
    

    City = mapper.readValue(jsonUrl, City.class);
    

    您应该将对象类赋予readValue 方法。

    【讨论】:

    • 非常感谢@jdiver。我应用了你所说的,但它仍然给我这个错误: 在这一行有多个标记 - ObjectMapper 类型中的方法 readValue(JsonParser, Class) 不适用于参数 (URI, Class) - ObjectMapper 类型中的方法 readValue(JsonParser, Class) 不适用于参数 (URI, Class)
    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多