1

我正在创建一个 JSON 文件并将其保存到一个位置。第一次创建文件时,没有问题。但是,如果该文件存在并且它应该更新同一位置的现有 JSON 文件,我将收到以下异常:

异常:

Unexpected character (-) at position 138.
at org.json.simple.parser.Yylex.yylex(Unknown Source)
at org.json.simple.parser.JSONParser.nextToken(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at nbclearn.edgate.executorService.ExecService$1.run(ExecService.java:116)
at 
Unexpected character (d) at position 123.
at org.json.simple.parser.Yylex.yylex(Unknown Source)
at org.json.simple.parser.JSONParser.nextToken(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at nbclearn.edgate.executorService.ExecService$1.run(ExecService.java:116)
at 

Unexpected token END OF FILE at position 0.
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at nbclearn.edgate.executorService.ExecService$1.run(ExecService.java:116)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

文件编写:

BufferedWriter writer = new BufferedWriter(
                                        new OutputStreamWriter(new FileOutputStream("/Users/smo/Desktop/Ed/"+id+".json"),
                                                "utf-8"));
                                try {
                                    writer.write(jsonDATA);
                                    }

                                    System.out.println("Done");
                                } finally {
                                    writer.close();
                                }

我正在使用 org.simple libraru 来查看该位置是否存在 json 文件,否则它应该创建新文件。如果它存在,它应该更新它。

检查 JSON 文件的代码:

Path p = Paths.get("/Users/smo/Desktop/Ed/"+id + ".json");
                                boolean exists = Files.exists(p);
                                if(exists){
                                Object obj = parser.parse(new FileReader(
                                        "/Users/smo/Desktop/Ed/"+id + ".json"));
                                org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) obj;

JSON数据:

{“id1-position-backstik”: [“4289998”, "422212998", "49998", “638737”],”sum”:2a7c0d20-baf1-4c37-8ac5-eb14198eed06,”position”:1 }


                                }

任何人都可以请建议。在此先感谢。

4

0 回答 0