简体   繁体   English

Java-Json File Parser问题

[英]Java - Json File Parser issue

I am trying to read two objects in Json file using Java JSONParser but I am getting a below issue while parsing a Json file. 我正在尝试使用Java JSONParser读取Json文件中的两个对象,但是在解析Json文件时遇到了以下问题。 I have provided Java code and Json file content for your reference. 我提供了Java代码和Json文件内容供您参考。

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 Code: 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 File: 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"
    }
}

The JSON you are sending in not correct. 您发送的JSON不正确。 It consist of 2 JSON object one after another which is illegal. 它由两个非法的JSON对象组成。 Either you have to add both object in JSONArray, get individual object from array and manipulate it. 您必须在JSONArray中添加两个对象,从数组中获取单个对象并对其进行操作。 Or you can split Json string at "}{" and then convert it to json object for further calculation. 或者,您可以在“} {”处拆分Json字符串,然后将其转换为json对象以进行进一步的计算。

PS i know the second approach is dirty but this could make make someone day. 附言:我知道第二种方法是肮脏的,但这可能会让某人感到高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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