繁体   English   中英

Java-Json File Parser问题

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM