[英]Java - Json File Parser issue
我正在嘗試使用Java JSONParser讀取Json文件中的兩個對象,但是在解析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不正確。 它由兩個非法的JSON對象組成。 您必須在JSONArray中添加兩個對象,從數組中獲取單個對象並對其進行操作。 或者,您可以在“} {”處拆分Json字符串,然后將其轉換為json對象以進行進一步的計算。
附言:我知道第二種方法是骯臟的,但這可能會讓某人感到高興。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.