Ich versuche, zwei Objekte in JSON-Datei mit Java JSONParser zu lesen, aber ich bekomme ein unten Problem beim Parsen einer JSON-Datei. Ich habe Java-Code und JSON-Datei-Inhalt für Ihre Referenz zur Verfügung gestellt.Java - Json File Parser Problem
Ausgabe:
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-Code:
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 Datei:
{
"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"
}
}
Haben Sie Ihren JSON mit Online-Tools validiert? '} {' ist nicht korrekt. Ich mag jsonlint – AxelH
Ihre JSON-Datei ist nicht gültig: Es enthält 2 JSON-Objekte nacheinander. – pozs