A
A
astral10042020-01-07 20:12:48
Java
astral1004, 2020-01-07 20:12:48

How to add a save to a file?

Good afternoon. help me implement saving to a file what is output to the console with the line res.items.add .....
Here is part of my code

String ConvertToUniversalJson(String jsonVK) {
        String json = "srwe";


        ObjectMapper mapper = new ObjectMapper();//инициализация библиотеки Jackson
        Example ex = null;
        try {
            ex = (Example) mapper.readValue(jsonVK, Example.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JsonResult res = new JsonResult();
        for (Item item : ex.getResponse().getItems()) {
            res.items.add(new JsonResultItem(item.getId().toString(), item.getSizes().get(4).getUrl().toString()));
            
        }
        //System.out.println(item.getId().toString());

        try {
            json = mapper.writeValueAsString(res);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

Console output
{"items":[{"id":"457239026","url":"https://sun9-69.userapi.com/c853428/v853428590/.....

I want to save to json file. help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2020-01-07
kuzmin @sergueik

@astral1004
try googling java write text to file example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question