I am trying to use GSON to convert a Json string to an object. When I load the string from a file like this:
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "jsonTest");
BufferedReader br = new BufferedReader(new FileReader(f));
String Json = br.readLine();
br.close();
Gson gson = new Gson();
dreFieldArray = gson.fromJson(Json, DreField[].class);
It works fine. However, if I try to use a string resource instead like this:
String Json = this.getString(R.string.NewFileData);
Gson gson = new Gson();
dreFieldArray = gson.fromJson(Json, DreField[].class);
I get an EOFexception. The string in resources was copied from the file, so it should be identical. I read that this exception can occur if there are extra characters after the object, but I don’t see any. Any ideas on what is going wrong here?
getString()will not display all special characters. You might have to escape them.Log the content of
R.string.NewFileData, see the missing characters and then escape them in thestrings.xml(put a “\” in front of the removed characters)