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.
您必须在第二个参数中指定Class,
return mapper.readValue(new File("/tmp/notes.json"), new TypeReference<List<String>>(){});
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.