java 调用 url json数据库,将json URL导入Java并使用jackson库进行解析

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

}

}

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 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

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

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.

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;

}

}

Thanks in advance

解决方案

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

And then do as @jdiver says:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值