我想讀取使用Java JSONParser的Json文件中的兩個對象,但在解析Json文件時遇到了以下問題。我提供了Java代碼和Json文件內容供您參考。Java - Json文件解析器問題
問題:
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