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

其他提示

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

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

with

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

You should give object class to readValue method.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top