If you look at the Scanner.java class, you can see spaces that are treated as newline characters.
public Scanner(InputStream source) { this(new InputStreamReader(source), WHITESPACE_PATTERN); }
You need to set the line separator as
s.useDelimiter(System.getProperty("line.separator"));
I was able to replicate and fix the problem, below is the code -
Json
{ name:"Stack-overflow", url:"http://www.stack-overflow .com" }
And here is the class
public class GsonTest { public static void main(String[] args) throws FileNotFoundException { StringBuilder sb = new StringBuilder(); Scanner s = new Scanner(new FileReader(new File( "/Users/chatar/Documents/dev/projects/stack-overflow/stack-overflow/bin/C:/User/waithaka/Documents/duka.json"))); s.useDelimiter(System.getProperty("line.separator")); while (s.hasNext()) { String nxt = s.next(); sb.append(nxt); } s.close(); GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); String jstring = sb.toString(); Result results = gson.fromJson(jstring, Result.class); System.out.println("The item is " + results); } }
source share