Question

I am trying to write a project that imports a weather API json to java and I am getting a conflict error. In the below you can see part of the json that I need the java object to be generated for it and it needs to be a list object class. First of all I am really confused about how to write an object class as a list(I searched for this problem but no answer or tutorial was precise enough for me) and second of all the name of the class that should be "list" to match the json (as I understand) produces a conflict with the import java.util.List;

"list": [

{
    "dt": 1399950000,
    "main": {
        "temp": 287.82,
        "temp_min": 287.82,
        "temp_max": 287.82,
        "pressure": 923.74,
        "sea_level": 1018.93,
        "grnd_level": 923.74,
        "humidity": 100,
        "temp_kf": 0
    },
    "weather": [
        {
            "id": 501,
            "main": "Rain",
            "description": "moderate rain",
            "icon": "10d"
        }
    ],
    "clouds": {
        "all": 92
    },
    "wind": {
        "speed": 0.51,
        "deg": 226.005
    },
    "rain": {
        "3h": 6
    },
    "sys": {
        "pod": "d"
    },
    "dt_txt": "2014-05-13 03:00:00"
},
{
    "dt": 1399960800,
    "main": {
        "temp": 291.36,
        "temp_min": 291.358,
        "temp_max": 291.36,
        "pressure": 921.65,
        "sea_level": 1016.09,
        "grnd_level": 921.65,
        "humidity": 87,
        "temp_kf": 0
    },
    "weather": [
        {
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10d"
        }
    ],
    "clouds": {
        "all": 20
    },
    "wind": {
        "speed": 0.87,
        "deg": 12.0018
    },
    "rain": {
        "3h": 0.5
    },
    "sys": {
        "pod": "d"
    },

As you can see inside the json I have a list object that I need to map it into java object. The java class with list object is as below:

package weather.data;

import java.util.ArrayList;
import java.util.List;

public class List{

    List list = new ArrayList();
    private Main main;
    private Clouds clouds;
    private Number dt;
    private Weather weather;
    private Wind wind;
    private Rain rain;
    private Sys sys;
    private String dt_txt;

    public Main getMain(){
        return this.main;
    }
    public void setMain(Main main){
        this.main = main;
    }

    ...
       //The rest of setters and getters
}

I get this error in front of the import java.util.List: The import java.util.List conflicts with a type defined in the same file and this error in front of List list = new ArrayList(); Multiple markers at this line - ArrayList is a raw type. References to generic type ArrayList should be parameterized - Type mismatch: cannot convert from ArrayList to List - ArrayList cannot be resolved to a type

I also tried to change the name of the object to list1 but when I tried to run the code I faced another error: Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of weather.data.list1 out of START_ARRAY token at [Source: https://gist.githubusercontent.com/anonymous/4b32c7ef1ceb5dd48bf5/raw/ef1987551faa3fb61473bb0e7aad70a228dc36d6/gistfile1.txt; line: 15, column: 14] (through reference chain: weather.data.Jweather["list"]) at ...

Any help would be highly appreciated.

Était-ce utile?

La solution 2

It looks like you're trying to implement your own linked list class. Better just to use the LinkedList class that comes in the JDK, if that is the functionality that you want, and have a Readings class (or Weather, or whatever name would be meaningful to you) that stores all the numbers, and the temperature and so on, but not the "next" node of the list.

Autres conseils

Your class named List is shadowing java.util.List, so your field is of the type of this class; that's what the compiler is telling you. Avoid naming your classes the same simple names as core API classes, and use generic parameters on collections.

Please change your class name and run it will work fine,as your class-name and List name are same you are getting that error.

Name of your class is List while jre has its own class List in java.util.List which you are importing.

Change the name of your class, it cant be any java's API name

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top