Convert Java List to Json String using Jackson

Post Banner
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class JavaListToJsonString {

    public static void main(String[] args) {
        try {

            List<String> insuranceCompaniesList = new ArrayList<>();
            insuranceCompaniesList.add("Chubb Ltd.");
            insuranceCompaniesList.add("Travelers Companies Inc.");
            insuranceCompaniesList.add("Liberty Mutual");
            insuranceCompaniesList.add("Zurich Insurance Group");
            insuranceCompaniesList.add("Berkshire Hathaway Inc.");
            insuranceCompaniesList.add("American International Group (AIG)");

            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writeValueAsString(insuranceCompaniesList);

        } catch (IOException e) {
        } catch (Exception e) {
  "Chubb Ltd.",
  "Travelers Companies Inc.",
  "Liberty Mutual",
  "Zurich Insurance Group",
  "Berkshire Hathaway Inc.",
  "American International Group (AIG)"

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap