0

I am writing javafx app I try to sava and load data using JSON @FXML private void OpenEvent(ActionEvent event) throws IOException, ParseException, Exception {

    String jsonString = new String();

    FileReader fileReader = new FileReader("test.json");
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    System.out.println("Check open event here");

    String inputLine;
    while ((inputLine = bufferedReader.readLine()) != null) {
        jsonString += inputLine;
    }
    bufferedReader.close();

    System.out.println(jsonString);

    //GOOD HERE
    JSONArray jlist;
    try {
        jlist = parseJsonArray(jsonString);
    } catch (Exception ex) {
        throw ex;
    }

    for (Object e : jlist) {
        try {
            JSONObject jentryParsed = (JSONObject) e;
            LocalEvent entry = new LocalEvent();
            entry.initFromJsonString(jentryParsed.toJSONString());

        } catch (Exception ex) {
            throw ex;
        }

    }
}

public JSONArray parseJsonArray(String jsonString) throws Exception {
    JSONArray jlist;
    JSONParser parser = new JSONParser();

    System.out.println("Check parse here");
    System.out.println(jsonString);

    try {
        jlist = (JSONArray) parser.parse(jsonString);
    } catch (Exception ex) {
        throw ex;
    }

    System.out.println("parsed finished");

    if (jlist == null) {
        System.out.println("jlist is null");
        return null;
    } else {
        return jlist;
    }
}

and here is my JSON file

[{"Description":"11111","Name":"11111","Datetime":2016-04-27},{"Description":"2222","Name":"2222","Datetime":2016-04-14}]

error:

Caused by: Unexpected token VALUE(-4) at position 54.
at org.json.simple.parser.JSONParser.parse(JSONParser.java:257)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at todolist.MainController.parseJsonArray(MainController.java:276)
at todolist.MainController.OpenEvent(MainController.java:250)
... 50 more

It seems the json parse is failed. is here anything wrong with my JSON file? Thanks!!!!!!! or the parse cannot recognize "-" in the datetime??

  • Possible duplicate of [The "right" JSON date format](http://stackoverflow.com/questions/10286204/the-right-json-date-format) – Itai Apr 30 '16 at 17:25
  • JSON has no Date type, you have to put a string (with quotes) and parse it at either end. See flagged question. – Itai Apr 30 '16 at 17:26

0 Answers0