1

I am trying to pass a file to JSON parser on the unix box like this:

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

This will run on the unix box. But I get the following error :

Unexpected character (/) at position 0. 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 org.json.simple.parser.JSONParser.parse(Unknown Source) at com.ca.service.ModifyEventJsonFile.replaceActorJsonFile(ModifyEventJsonFile.java:21) at com.ca.controller.ChefController.saveSchzScript(ChefController.java:139) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497)

How to enter the path correctly?

4
  • 3
    I suspect that parse.parse(String) assumes that the String is a JSON String, not a file reference, try parser.parse(new File("/opt/Scheherazade/hegrid.git/hegrid-web/EventsJson.json")); instead Commented Jul 6, 2015 at 7:05
  • I cannot use parser.parse(new File("/opt/Scheherazade/hegrid.git/hegrid-web/EventsJson.json")). parser.parse accepts a string so I have to make it as " parser.parse(String.valueOf(new File("/opt/Scheherazade/hegrid.git/hegrid-web/EventsJson.json")));" Commented Jul 6, 2015 at 7:07
  • What parser did you use? Commented Jul 6, 2015 at 7:08
  • 1
    No, sorry, you're required to provide Reader, had to dig the source out... Commented Jul 6, 2015 at 7:10

1 Answer 1

2

Based on the examples found here and the source code, JSONParser#parse(String) expects the String value to be a JSON String and not a file reference.

The only only other method available is JSONParser#parse(Reader), something like

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

should get a little closer to your goal

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.