<object> 列表到 JSONArray



我需要从要发送给我的webService的对象列表中创建一个JSONARAY,然后用javaScript获取这个JSONARY并再次创建对象。

观察:这些对象在JSON文件中。

public static List<Funcionario> pegaFuncionario() throws JsonParseException, JsonMappingException, IOException {
list = new ArrayList<>();
mapper = new ObjectMapper();
reader = new BufferedReader(new FileReader("arquivos/usuarios"));
while ((line = reader.readLine()) != null) {
list.add(mapper.readValue(line, Funcionario.class));
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}

我认为,因为您需要通过HTTP将其发送到web服务,所以您需要一个JSON数组字符串。Jackson可以将对象写入JSON字符串,并将List视为JSONArray:

ObjectMapper mapper = new ObjectMapper();
List<Funcionario> list = pegaFuncionario();
String jsonArray = mapper.writeValueAsString(list);

最新更新