Я пытаюсь прочитать два объекта в Json-файле с помощью Java JSONParser, но при анализе Json-файла у меня возникает проблема ниже. Я предоставил Java-код и содержимое Json-файла для вашей справки.Java-Json File Parser issue
Выпуск:
Unexpected token LEFT BRACE({) at position 286.
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
Java код:
public static void main(String[] args) throws Throwable {
JSONParser parser = new JSONParser();
JSONObject a = null;
Connection con = null;
Statement stmt = null;
try {
Object obj = parser.parse(new FileReader("C:\\Users\\mhq175\\workspace2\\JCucumber\\JSON_FILES\\Datafile.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONObject structure = (JSONObject) jsonObject.get("MessageContext");
JSONObject structure_2 = (JSONObject) jsonObject.get("MessageContext1");
con = postconn.getConnection();
String entireFileText = "INSERT INTO Events"
+ " VALUES ('"+ structure + "','"+ structure_2 + "');";
System.out.println(entireFileText);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(entireFileText);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}
}
Json Файл:
{
"MessageContext": {
"field1": "VALUE1",
"field2": "VALUE2",
"field3": "VALUE3",
"field4": "VALUE4",
"field5": "VALUE5"
},
"MessageContext1": {
"field1": "VALUE1",
"field2": "VALUE2",
"field3": "VALUE3",
"field4": "VALUE4",
"field5": "VALUE5"
}
}{
"MessageContext": {
"field1": "VALUE1",
"field2": "VALUE2",
"field3": "VALUE3",
"field4": "VALUE4",
"field5": "VALUE5"
},
"MessageContext1": {
"field1": "VALUE1",
"field2": "VALUE2",
"field3": "VALUE3",
"field4": "VALUE4",
"field5": "VALUE5"
}
}
Вы проверили свой JSON с помощью любых онлайн-инструментов? '} {' неверно. Мне нравится jsonlint – AxelH
Ваш json-файл недопустим: он содержит 2 json-объекта друг за другом. – pozs