0

I'm trying to use gson to parse a JSON file containing an array of objects. I'm getting a "Exception in thread "main" com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 2" error on this line of code:

  RecipeList[] myRecipe = gson.fromJson(theLine, RecipeList[].class);

I am a bit confused about using gson so I don't think I've set things up properly. The data is in this format:

[  {
"id": 10259,
"cuisine": "greek",
"ingredients": [
  "romaine lettuce",
  "black olives"
]  }, {
"id": 25693,
"cuisine": "southern_us",
"ingredients": [
  "plain flour",
  "ground pepper",
  "salt",
  "tomatoes",
  "ground black pepper",
  "thyme"
]  }]

the code trying to read that is:

inputStream = new FileReader("filepath\\file.json");
BufferedReader myReader = new BufferedReader(inputStream);
theLine = myReader.readLine();

Gson gson = new Gson();
RecipeList[] myRecipe = gson.fromJson(theLine, RecipeList[].class);

my RecipeList class, which was intended to store the array of recipe objects in the json file (but I think this must be wrong)

ArrayList<Recipe> recipeArray;

public RecipeList (Recipe recipObj){
    recipeArray.add(recipObj);
}

And my Recipe class, for the creation of each recipe object in the json array:

String recipeID;
String cuisine;
ArrayList<String> ingredients;


public Recipe (String id, String cuisine, ArrayList<String> ingredients){
    this.recipeID = id;
    this.cuisine = cuisine;
    for (int k = 0; k < ingredients.size(); k++){
        this.ingredients.add(ingredients.get(k));
    }
}  

I appreciate any help. I'm a bit confused over the reading of the json text using gson and how to create the individual objects from that array.

thanks Rebecca

1 Answer 1

1

theLine = myReader.readLine();

you need to read all lines from file until eof.

ex:

    br = new BufferedReader(new FileReader("C:\\testing.txt"));

String line="";
                while ((line = br.readLine()) != null) {
                    theLine+=line;
                }
Sign up to request clarification or add additional context in comments.

5 Comments

That seems to have helped clear the error message. I don't know if the rest is working as its still reading that file (its quite large). Given that its a large file is there any way to process it without having it become 1 long string? What is the line delimiter in a json file? Is it after each object in the array, or something arbitrary?
Actually you have to read complete file. Format of file dos not change whether its JSON or other file. You need a complete JSON before you parse it to GSON. I can't find any help in that. But you can use StringBuilder instead of String if you think its occupying java heap a lot.
I think you can parse GSON from InputStream Instead of using String that also can help. Gson gson = new Gson(); Reader reader = new InputStreamReader(source); SearchResponse response = gson.fromJson(reader,SearchResponse.class); Here source will be your file.
Thanks. I did try this using "}," as the delimiter as when I output several lines it seemed that it was on a line of its own: while ((line = myReader.readLine()) != null) { while ((line.compareTo("},") != 0)) { theLine += line; } - But it just concatenated "{" and didn't match anything else
Just tried to edit my above comment to say that the compareTo wasn't working as the string contained some extra char, probably whitespace. Matching as a substring worked. I might try that to see if I can extract a JSON object 1 object at a time, then pass that to gson to deserialise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.