【发布时间】: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;
}
}
提前致谢
【问题讨论】: