将json URL导入java并使用jackson库解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23479332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:17:28  来源:igfitidea点击:

Import json URL to java and parse it using Hymanson library

javajsonHymanson

提问by zahra

I am trying to read a json link in java and parse it so I can use it for other matters, but the problem is that I get errors that I really don't know waht to do about them. Here is the code:

我正在尝试在 Java 中读取 json 链接并解析它,以便我可以将它用于其他事项,但问题是我遇到了我真的不知道该怎么做的错误。这是代码:

package weather.data;

import weather.data;

import com.fasterxml.Hymanson.core.JsonParseException;
//import com.fasterxml.Hymanson.annotation.JsonMappingException;
import com.fasterxml.Hymanson.databind.ObjectMapper;
import com.fasterxml.Hymanson.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);
    }

}

Here are the errors: about import weather.data:Multiple markers at this line - Only a type can be imported. weather.data resolves to a package - The import weather.data is never used

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

about City = mapper.readValue(jsonUrl, data.class):Multiple markers at this line - data cannot be resolved to a type - City cannot be resolved to a variable - The method readValue(JsonParser, Class) in the type ObjectMapper is not applicable for the arguments (URI, Class) - The method readValue(JsonParser, Class) in the type ObjectMapper is not applicable for the arguments (URI, Class) - City cannot be resolved to a variable - data cannot be resolved to a type

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

Any Ideas? Also I really do not understand the concept of using mapper.readValue. would any one help me please?

有任何想法吗?另外我真的不明白使用mapper.readValue的概念。有人可以帮我吗?

Note: I have used json gen to generate the data objects inside the link and now I have object data java files: City, Coord, List1, Temp and Weather with the following contents.

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

For City.java as a sample:

以 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;
    }
}

Thanks in advance

提前致谢

采纳答案by jcragun

You need to import weather.data.City(specific class) instead of import weather.data(a package).

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

And then do as @jdiver says:

然后按照@jdiver 说的做:

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

回答by jdiver

If gistfile1.txtfile in the URL includes a JSON object of City, then replace the line

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

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

with

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

You should give object class to readValuemethod.

您应该为readValue方法提供对象类。