0

I am trying to convert a json file to a list of strings, but it is throwing expected -> error

I've already tried TypeReference<List<String>> List<String> but it throws a different error each time. Look at line 28 for the error location.

Error Message:

[error] /home/willroy/Code/playframework/database/app/controllers/HomeController.java:28:1: -> expected [error] return mapper.readValue(new File("/tmp/notes.json"), List(){}); [error] (Compile / compileIncremental) javac returned non-zero exit code [error] Total time: 0 s, completed 12-Jun-2019 11:05:01

public class HomeController extends Controller {
  public Result get() {
    return ok("")
    WS.url("localhost:9000")
      .seteContentType("/")
      .post(getJson());
  } 

  public Result post(String text) { 
    List<String> noteJson = getJson();
    noteJson.append(text);
    return ok("");  
  }

  private List<String> getJson() {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(new File("/tmp/notes.json"), List<String>(){});
  }    

  private void saveJson(List<String> noteJson) {
    FileWriter file = new FileWriter("/tmp/notes.json");
    file.write(notJson.toJSONString());
  } 

}

SOLVED:

Did not import enough at the top of the file (File, List, etc...) XD.

Will
  • 23
  • 1
  • 5
  • you get compilation error cause second argument in `readValue` function should be class and not an instance of List. also take a look at this answer https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects – Aliaksei Bulhak Jun 12 '19 at 10:16

2 Answers2

0

You must specify the Class in the second argument,

return mapper.readValue(new File("/tmp/notes.json"), new TypeReference<List<String>>(){});
Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45
0

Just change your code by using this example below :

private List<String> getJson() {
    List<String> texts = new ArrayList<String>();
    ObjectMapper mapper = new ObjectMapper();
    final JsonNode nodes = objectMapper.readTree(new File("/tmp/notes.json")).get("list");
    if (nodes.isArray()) {
        for (final JsonNode node : nodes) {
            texts.add(node.get("property").asText());
        }
    }
    return texts;
}
Anish B.
  • 9,111
  • 3
  • 21
  • 41
  • 1
    Thanks I will try this! – Will Jun 12 '19 at 11:59
  • Tried it and it fixed the error, however I got a load of other errors "cannot find symbol" for a load of lines (33, 28, 10, 31, 26) – Will Jun 12 '19 at 16:03
  • But what are you trying to access ? Can you just add json to your question ? – Anish B. Jun 12 '19 at 16:05
  • Wait, would it matter if the json file was empty? Haven't added anything to it yet. The file does exist however. – Will Jun 12 '19 at 16:06
  • If json is empty, then it may be objectmapper will cause a problem. – Anish B. Jun 12 '19 at 16:11
  • Just tried adding items to json file (["one","two","three"]) and got same error. – Will Jun 12 '19 at 16:13
  • Nope does the same thing. – Will Jun 12 '19 at 16:35
  • Still no, must be something else then, it seems to throw a "cannot find symbol" error every line that has List, could I be doing something wrong there? – Will Jun 12 '19 at 16:45
  • 1
    Okay, I just have not imported anything (List, File, etc) and It seems to work fine now. Thanks for the help, it is much appreciated! – Will Jun 12 '19 at 16:53