13
Сен
2021

Gson бросает ошибку при компиляции приложения из консоли: Expected BEGIN_OBJECT but was STRING at line 1 column 1

Прошу помочь разобраться с возникшей проблемой! При компиляции программы через консоль Gson бросает ошибку Expected BEGIN_OBJECT but was STRING at line 1 column 1. При компиляции в IDE всё работает. Я полагаю, что проблема в методе fromJson класса GsonParser. Но не муго разобраться в чем именно.

Структура проекта: https://github.com/ArtemSadovskiy/VladivostokAndTelAviv.git

JSON:

{
  "tickets": [{
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "16:20",
    "arrival_date": "12.05.18",
    "arrival_time": "22:10",
    "carrier": "TK",
    "stops": 3,
    "price": 12400
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "17:20",
    "arrival_date": "12.05.18",
    "arrival_time": "23:50",
    "carrier": "S7",
    "stops": 1,
    "price": 13100
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "12:10",
    "arrival_date": "12.05.18",
    "arrival_time": "18:10",
    "carrier": "SU",
    "stops": 0,
    "price": 15300
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "17:00",
    "arrival_date": "12.05.18",
    "arrival_time": "23:30",
    "carrier": "TK",
    "stops": 2,
    "price": 11000
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "12:10",
    "arrival_date": "12.05.18",
    "arrival_time": "20:15",
    "carrier": "BA",
    "stops": 3,
    "price": 13400
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "9:40",
    "arrival_date": "12.05.18",
    "arrival_time": "19:25",
    "carrier": "SU",
    "stops": 3,
    "price": 12450
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "17:10",
    "arrival_date": "12.05.18",
    "arrival_time": "23:45",
    "carrier": "TK",
    "stops": 1,
    "price": 13600
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "6:10",
    "arrival_date": "12.05.18",
    "arrival_time": "15:25",
    "carrier": "TK",
    "stops": 0,
    "price": 14250
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "16:50",
    "arrival_date": "12.05.18",
    "arrival_time": "23:35",
    "carrier": "SU",
    "stops": 1,
    "price": 16700
  }, {
    "origin": "VVO",
    "origin_name": "Владивосток",
    "destination": "TLV",
    "destination_name": "Тель-Авив",
    "departure_date": "12.05.18",
    "departure_time": "6:10",
    "arrival_date": "12.05.18",
    "arrival_time": "16:15",
    "carrier": "S7",
    "stops": 0,
    "price": 17400
  }]
}

Класс Main:

package com.testtask;

import com.testtask.model.Root;
import com.testtask.model.Ticket;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

    public static void main(String[] args){

        GsonParser parser = new GsonParser();

        Root root = parser.parser();

        ArrayList <Integer> array = new ArrayList<>();

        for (Ticket tickets : root.getTicket()){
        array.add(flightTime(tickets));
        }

        System.out.println("Cреднее время полета между городами Владивосток и Тель-Авив " + Duration.ofMinutes(averageValue(array)));
        System.out.println("90-й процентиль времени полета между городами  Владивосток и Тель-Авив " + Duration.ofMinutes(percentile(array, 90.0)));
    }

    public static int averageValue(List<Integer> array){
        int value = array.stream().mapToInt(a -> a).sum()/array.size();
        return  value;
    }

    public static int percentile(List<Integer> array, double percentile) {
        Collections.sort(array);
        int index = (int) Math.ceil(percentile / 100.0 * array.size());
        return array.get(index - 1);
    }

    public static int flightTime (Ticket tickets){
        DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uu H:mm" );

        String input = tickets.getDeparture_date() + " " + tickets.getDeparture_time();
        LocalDateTime ldt = LocalDateTime.parse(input , f);
        ZoneId z = ZoneId.of("Asia/Vladivostok");
        ZonedDateTime zdt = ldt.atZone(z);

        String input2 = tickets.getArrival_date() + " " + tickets.getArrival_time();
        LocalDateTime ldt2 = LocalDateTime.parse(input2 , f);
        ZoneId z2 = ZoneId.of("Etc/GMT-3");
        ZonedDateTime zdt2 = ldt2.atZone(z2);

        return (int)ChronoUnit.MINUTES.between(zdt, zdt2);
    }
}

Парсер:

package com.testtask;

import com.google.gson.Gson;
import com.testtask.model.Root;

import java.io.FileReader;

public class GsonParser {
    public Root parser(){

        Gson gson = new Gson();

        try(FileReader reader = new FileReader("tickets.json")){
            Root root = gson.fromJson(reader, Root.class);
            return root;
        } catch (Exception e){
            System.out.println("Parsing error " + e.toString());
        }
        return null;
    }
}

Класс Root:

package com.testtask.model;

import java.util.List;

public class Root {
    private List<Ticket> tickets;

    public List<Ticket> getTicket() {
        return tickets;
    }

    public void setTicket(List<Ticket> ticket) {
        this.tickets = ticket;
    }


    @Override
    public String toString() {
        return "Root{" +
                "ticket=" + tickets +
                '}';
    }
}

Класс Ticket:

package com.testtask.model;


public class Ticket {
    private String origin;
    private String origin_name;
    private String destination;
    private String destination_name;
    private String departure_date;
    private String departure_time;
    private String arrival_date;
    private String arrival_time;
    private String carrier;
    private int stops;
    private int price;

    public String getOrigin() {
        return origin;
    }

    public String getOrigin_name() {
        return origin_name;
    }

    public String getDestination() {
        return destination;
    }

    public String getDestination_name() {
        return destination_name;
    }

    public String getDeparture_date() {
        return departure_date;
    }

    public String getDeparture_time() {
        return departure_time;
    }

    public String getArrival_date() {
        return arrival_date;
    }

    public String getArrival_time() {
        return arrival_time;
    }

    public String getCarrier() {
        return carrier;
    }

    public int getStops() {
        return stops;
    }

    public int getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Ticket{" +
                "origin='" + origin + '\'' +
                ", origin_name='" + origin_name + '\'' +
                ", destination='" + destination + '\'' +
                ", destination_name='" + destination_name + '\'' +
                ", departure_date='" + departure_date + '\'' +
                ", departure_time='" + departure_time + '\'' +
                ", arrival_date='" + arrival_date + '\'' +
                ", arrival_time='" + arrival_time + '\'' +
                ", carrier='" + carrier + '\'' +
                ", stops=" + stops +
                ", price=" + price +
                '}';
    }
}

Источник: https://ru.stackoverflow.com/questions/1328020/gson-%D0%B1%D1%80%D0%BE%D1%81%D0%B0%D0%B5%D1%82-%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D1%83-%D0%BF%D1%80%D0%B8-%D0%BA%D0%BE%D0%BC%D0%BF%D0%B8%D0%BB%D1%8F%D1%86%D0%B8%D0%B8-%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D1%8F-%D0%B8%D0%B7-%D0%BA%D0%BE%D0%BD%D1%81%D0%BE%D0%BB%D0%B8-expected-begin-object

Тебе может это понравится...

Добавить комментарий