Java 位置9处的意外字符(j)

Java 位置9处的意外字符(j),java,json,Java,Json,我按照本教程阅读了之前创建的json文件 我加入了JavaSwing和jFile选择器。 以下是代码: fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Select a file to open"); JSONParser jsonParser = new JSONParser(); int returnValue = fileChooser.showOpenDialog

我按照本教程阅读了之前创建的json文件

我加入了JavaSwing和jFile选择器。 以下是代码:

fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Select a file to open");
        JSONParser jsonParser = new JSONParser();
        int returnValue = fileChooser.showOpenDialog(null);
        // int returnValue = jfc.showSaveDialog(null);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
                        try{
                            JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader(selectedFile.toString()));
                            JTree tree = (JTree) jsonObject.get("JTree");
                            Connection conn = (Connection) jsonObject.get("Connection");
                            new Principale(tree, conn). setVisible(false);
                        }
                        catch(Exception e){
                            e.printStackTrace();
                        }

        }
如果我尝试打印selectedFile.toString(),它将返回正确的路径,但当我运行项目时,当我获取.parse()时,我会出现此错误:

位置9处的意外字符(j)。在 org.json.simple.parser.Yylex.Yylex(Yylex.java:610)位于 org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)位于 org.json.simple.parser.JSONParser.parse(JSONParser.java:118)位于 org.json.simple.parser.JSONParser.parse(JSONParser.java:92)

这是我根据本教程使用java swing中的另一个类创建的.json文件:

如果有人能告诉我如何解决,我将非常感激

编辑

这就是我如何使用java swing ad jFileChooser创建json文件的

JFrame parentFrame = new JFrame();
        //Creating a JSONObject object
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("JTree", (JTree) tree);
        jsonObject.put("Connection", (Connection) conn);
        saveChooser = new JFileChooser();
        saveChooser.setDialogTitle("Specify a file to save");   

        int userSelection = saveChooser.showSaveDialog(parentFrame);

        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToSave = saveChooser.getSelectedFile();
            System.out.println("Save as file: " + fileToSave.getAbsolutePath());
            try{
                FileWriter myObj = new FileWriter(fileToSave.getAbsolutePath() + ".json");
                myObj.write(jsonObject.toJSONString());
                myObj.close();
            }
            catch(IOException ie){
                ie.printStackTrace();
            }
            catch(Exception e){
            e.printStackTrace();
            }
        }

问题不在于代码读取JSON的方式

真正的问题是您试图读取的文件不包含有效的JSON。JSON解析器无法理解它。(没有Java解析器能够理解它!)

假设的解决方案如下所示:

  • 修复生成文件的程序以生成有效的JSON

  • 编写自定义解析器来读取文件

但是,生成文件的代码似乎在
javax.swing.JTree
对象和JDBC
连接
对象上调用了
toString
,徒劳地试图序列化它们。不幸的是,这两个类在任何有用的意义上都不是可序列化的。即使可以解析文件中的文本,也无法重建
JTree
连接


所以你可能需要重新思考你想做什么。使用此代码和相应的序列化代码,您实际上想要实现什么?你到底想救什么。。。为什么。。。还有没有其他方法可以实现您的目标呢?

这最终是一个错误的JSON。应该是:
{“JTree”:“javax.swing.JTree”[,0,0167x818,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth。SynthBorder@3271444,flags=16777576,maximumSize=,minimumSize=,preferredSize=,editable=false,InvokeStopCellEditing=false,largeModel=false,rootVisible=true,rowHeight=0,scrollsOnExpand=true,showsRootHandles=true,toggleClickCount=2,visibleRowCount=20],“连接”:“oracle.jdbc.driver。T4CConnection@556a66b7}
因此,即使字段连接不是字符串,我也必须将其放入"?因为字段连接的类型是javax.sql.Connection,字段jTree的类型是javax.swing.JTreeNo,但在您的情况下,这是一个字符串抱歉,我没有指定…字段连接的类型是javax.sql.Connection,字段jTree的类型是javax.swing.jTree这些字段是java类型,绝对不支持by jsonSo,如果我想重建JTree和连接,我应该以什么格式保存这些信息?告诉我json是可能的方法之一,你可以在
JTree
中提取信息并保存它。我认为保存JDBC连接没有任何意义……这是不可能的。也许你可以ld保存一个JDBCURL+凭据,以便您可以创建另一个连接。“告诉我json是一种可能的连接方式”…嗯?
JFrame parentFrame = new JFrame();
        //Creating a JSONObject object
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("JTree", (JTree) tree);
        jsonObject.put("Connection", (Connection) conn);
        saveChooser = new JFileChooser();
        saveChooser.setDialogTitle("Specify a file to save");   

        int userSelection = saveChooser.showSaveDialog(parentFrame);

        if (userSelection == JFileChooser.APPROVE_OPTION) {
            File fileToSave = saveChooser.getSelectedFile();
            System.out.println("Save as file: " + fileToSave.getAbsolutePath());
            try{
                FileWriter myObj = new FileWriter(fileToSave.getAbsolutePath() + ".json");
                myObj.write(jsonObject.toJSONString());
                myObj.close();
            }
            catch(IOException ie){
                ie.printStackTrace();
            }
            catch(Exception e){
            e.printStackTrace();
            }
        }