0

I'm trying to adapt GSON library in my project. When I try to read JSON file and convert JSON string to JAVA object, this error happens:

**com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
    at com.google.gson.Gson.fromJson(Gson.java:900)
    at Company.getAllCars(Company.java:39)
    at Main.main(Main.java:12)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
    at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.Gson.fromJson(Gson.java:888)
    ... 2 more**

As I understand, it may be the problem with my .json file, but maybe someone can help to solve that? Below you can see my methods how I output JSON object to file and how I try to read that and my .json file. I will really appreciate your help. Thanks!

public void buyCar(Car car){
    Gson gson = new Gson();

    try{
        BufferedWriter bw = new BufferedWriter(new FileWriter("CarList.json",true));
        PrintWriter pw = new PrintWriter(bw);
        String json = gson.toJson(car);
        pw.println(json);
        pw.close();
        bw.close();
    }

    catch (Exception e) {
        e.printStackTrace();
    }
}

public LinkedList<Car> getAllCars(){
    Gson gson = new Gson();
    Type type = new TypeToken<LinkedList<Car>>(){}.getType();
    LinkedList<Car> carList = new LinkedList<>();

    try{
        JsonReader reader = new JsonReader(new FileReader(carListName));
        carList = gson.fromJson(reader,type);
    }

    catch (Exception e){
        e.printStackTrace();
    }
    return carList;
}

CarList.json

{
    "brand": "VW",
    "year": 2018
}{
    "brand": "BMW",
    "year": 2018
}{
    "brand": "Audi",
    "year": 2018
}{
    "brand": "Opel",
    "year": 2018
}{
    "brand": "Lambo",
    "year": 2018
}
Deividito
  • 23
  • 1
  • 9
  • Your JSON is "invalid". It's missing enclosing `[]`. Without them, it's a combination of root elements, which can't be parsed into a `List`. – Sotirios Delimanolis Mar 14 '18 at 19:36
  • How I could do, that `[]` are generated automatically? – Deividito Mar 14 '18 at 19:38
  • If you want to write multiple cars to the file, you'll need to overwrite the entire content of the file every time. – Sotirios Delimanolis Mar 14 '18 at 19:38
  • Is there any gson function that does so? – Deividito Mar 14 '18 at 19:57
  • No, you're essentially looking for a database. You first read from the file. If it contains nothing, write a JSON array with your single element. If it's not empty, read the content as a JSON array, add a car to it, then write it back. – Sotirios Delimanolis Mar 14 '18 at 19:58
  • Maybe it is possible to have json file like mine and I just read one line, convert json object to java object and add that to List? (When I want just to read all cars from file) – Deividito Mar 14 '18 at 20:05
  • You could try to find a parser that can read partial input and then read one JSON object at a time, while you collect them into a list. AFAIK Gson has to consume the entire stream you pass to it. – Sotirios Delimanolis Mar 14 '18 at 20:08
  • Thanks, I believe I've found a solution, litteraly same like you've mentioned. Thanks for your time! – Deividito Mar 14 '18 at 20:14

0 Answers0