将json URL导入到Java中并使用杰克逊库进行解析 [英] Import json URL to java and parse it using jackson library
问题描述
我正在尝试读取Java中的json链接并将其解析,以便可以将其用于其他事项,但是问题是我遇到了我真的不知道该怎么做的错误. 这是代码:
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:
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);
}
}
以下是错误: 关于导入weather.data:此行有多个标记 -只能导入一种类型. weather.data解析为 包裹 -从未使用过进口的weather.data
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
关于城市= mapper.readValue(jsonUrl,data.class):此行有多个标记 -无法将数据解析为一种类型 -无法将城市解析为变量 -类型为ObjectMapper的readValue(JsonParser,Class)方法不适用于 参数(URI,类) -类型为ObjectMapper的readValue(JsonParser,Class)方法不适用于 参数(URI,类) -无法将城市解析为变量 -数据无法解析为类型
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
有什么想法吗?我也真的不理解使用mapper.readValue的概念.有人可以帮我吗?
Any Ideas? Also I really do not understand the concept of using mapper.readValue. would any one help me please?
注意:我已经使用json gen生成了链接内的数据对象,现在我有了对象数据java文件:City,Coord,List1,Temp和Weather,其内容如下.
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.
以City.java为例:
For City.java as a sample:
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;
}
}
预先感谢
推荐答案
您需要import weather.data.City
(特定类)而不是import weather.data
(程序包).
You need to import weather.data.City
(specific class) instead of import weather.data
(a package).
然后按照@jdiver所说:
And then do as @jdiver says:
City city = mapper.readValue(jsonUrl, City.class);
这篇关于将json URL导入到Java中并使用杰克逊库进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!