我正在尝试将文件传递给unix框上的JSON解析器,如下所示:

JSONArray a = (JSONArray) parser.parse("/opt/Scheherazade/hegrid.git/hegrid-web/EventsJson.json");


这将在unix框上运行。但是我收到以下错误:


  位置0处出现意外字符(/)。
          在org.json.simple.parser.Yylex.yylex(未知来源)
          在org.json.simple.parser.JSONParser.nextToken(未知来源)
          在org.json.simple.parser.JSONParser.parse(未知来源)
          在org.json.simple.parser.JSONParser.parse(未知来源)
          在org.json.simple.parser.JSONParser.parse(未知来源)
          在com.ca.service.ModifyEventJsonFile.replaceActorJsonFile(ModifyEventJsonFile.java:21)
          在com.ca.controller.ChefController.saveSchzScript(ChefController.java:139)
          在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
          在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          在java.lang.reflect.Method.invoke(Method.java:497)


如何正确输入路径?

最佳答案

根据找到的示例here和源代码,JSONParser#parse(String)期望String值为JSON String而不是文件引用。

唯一可用的其他方法是JSONParser#parse(Reader),类似

try (FileReader reader = new FileReader(new File("/opt/Scheherazade/hegrid.git/hegrid-web/EventsJson.json"))) {
    JSONArray a = (JSONArray)parser.parse(reader);
}


应该更接近您的目标

09-10 03:39
查看更多