package jsonfiles;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingJSON {
public static void main(String[] args) throws IOException, ParseException {
String path = System.getProperty("user.dir")+"//test.json";
FileReader reader = new FileReader(path);
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject)parser.parse(reader);
System.out.println(json.toJSONString());
}
}
Error:
Exception in thread "main" Unexpected character (c) at position 9.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
at jsonfiles.ReadingJSON.main(ReadingJSON.java:17)
Fixed the missing quotes around keys in json file, ut now I get a different error (below):
Exception in thread "main" java.lang.ClassCastException: class org.json.simple.JSONArray cannot be cast to class org.json.simple.JSONObject (org.json.simple.JSONArray and org.json.simple.JSONObject are in unnamed module of loader 'app')
at jsonfiles.ReadingJSON.main(ReadingJSON.java:18)
my json file was missing the key originally, was able to fix my problem. Thanks!