类型不匹配:无法从List <root.stops>转换为

时间:2015-06-17 20:02:12

标签: java jackson

我正在尝试从JSON数组arrivalTime获取stops的值,但我收到此错误

  
      
  • 类型不匹配:无法从List转换为    图
  •   
  • arrivalTime无法解析或不是字段
  •   

我该如何解决?

我感谢任何帮助。

简单:

[
    {
        "stops": [
                   {
                     "arrival_time": {
                                      "sat": [ "04:41", "05:41","06:09"],
                                      "mon-fri": ["04:24","05:10","05:40"],
                                      "sun": ["07:20","08:20","09:20"]
                                     },
                     "stop_name": "Ravensbusch "
                   }
                ],
        "route": "2",
        "direction": "Bornkamp"
    }
] 

根类:

public class Root {

    public List<Stops> stops = new ArrayList<Stops>();
    public String route;
    private String direction;


    public static class Stops {

        public Map<String, String[]> arrivalTime = new HashMap<>();
        public String stopsName;

    }
}

注射类:

public class Injection {



        public static void main(String[] args) throws Exception {
            ObjectMapper mapper = new ObjectMapper();
            InputStream is = Injection.class.getResourceAsStream("/jsonfile.txt");

            List<Root> roots = mapper.readValue(is, TypeFactory.defaultInstance()
                    .constructCollectionType(List.class, Root.class));
            Root root = roots.get(0);

            System.out.println("route: " + root.route);
            //here is the error.
            Map<String, String[]> arrivalTimes = root.stops.arrivalTime;
            for (Map.Entry<String, String[]> entry: arrivalTimes.entrySet()) {
                System.out.println(entry.getKey());
                for (String time: entry.getValue()) {
                    System.out.println(time);
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

好像你缺少杰克逊的注释(特别是由于在Json和会员名称中,arrival_time和StopsName不同。这对我有用:

```

public class Stops {

    public Map<String, String[]> arrivalTime = new HashMap<>();
    public String stopsName;

    @JsonCreator
    public Stops(@JsonProperty("arrival_time") Map<String, String[]> arrivalTime,
                 @JsonProperty("stop_name") String stopsName) {
        this.arrivalTime = arrivalTime;
        this.stopsName = stopsName;
    }
}

public class Root {

    public List<Stops> stops = new ArrayList<Stops>();
    public String route;
    private String direction;

    @JsonCreator
    public Root(@JsonProperty("stops") List<Stops> stops,
                @JsonProperty("route") String route,
                @JsonProperty("direction") String direction) {
        this.stops = stops;
        this.route = route;
        this.direction = direction;
    }
}

public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    String is = "[{\"stops\":[{\"arrival_time\":{\"sat\":[\"04:41\",\"05:41\",\"06:09\"],\"mon-fri\":[\"04:24\",\"05:10\",\"05:40\"],\"sun\":[\"07:20\",\"08:20\",\"09:20\"]},\"stop_name\":\"Ravensbusch \"}],\"route\":\"2\",\"direction\":\"Bornkamp\"}]";
    List<Root> roots = mapper.readValue(is, TypeFactory.defaultInstance()
            .constructCollectionType(List.class, Root.class));
    // Rest of your code here
}

```

答案 1 :(得分:0)

如果您不想使用Json注释。请遵循以下我的发现。

public class Root {

    public List<Stops> stops = new ArrayList<Stops>();
    public String route;
    public String direction; //Fix: Make it public 

    public static class Stops {

        //Fix: Change attribute name to match input file arrival_time.
        public Map<String, String[]> arrival_time = new HashMap<String, String[]>();
        //Fix: Change attribute name to match input file stop_name.
        public String stop_name;

    }
}

public class Injection {

    public static void main(String[] args) throws Exception {

        ObjectMapper mapper = new ObjectMapper();
        //InputStream is = Injection.class.getResourceAsStream("./jsonfile.txt");
        //List<Root> roots = mapper.readValue(is, TypeFactory.defaultInstance().constructCollectionType(List.class, Root.class));

        //Fix: Read from file without input stream.
        List<Root> roots = mapper.readValue(new File("./jsonfile.txt"), TypeFactory.defaultInstance().constructCollectionType(List.class, Root.class));
        Root root = roots.get(0);

        System.out.println("route: " + root.route);

        //Fix: Read the first Item of the stops list or iterates it.
        Map<String, String[]> arrivalTimes = root.stops.get(0).arrival_time;
        for (Map.Entry<String, String[]> entry: arrivalTimes.entrySet()) {
            System.out.println(entry.getKey());
            for (String time: entry.getValue()) {
                System.out.println(time);
            }
        }
    }
}
相关问题