JSON. JacksonHome - FasterXML Wiki. Inspired by the quality and variety of XML tooling available for the Java platform (StAX, JAXB, etc.), the Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers. Introduction First things first: Jackson Tutorial gives you reasonable overview of basic usage Licensing page outlines what Open Source licenses are used with Jackson (ASL, LGPL) Jackson FAQ covers various other aspects, including common usage cases. Testimonials After introduction, you may want to know who is already using Jackson for production work: Sampling of current users gives an idea of Jackson adoption Get it! And then it may be time to check Jackson out: Download page has the artifacts (jars, sources) you need; specifically, 2.2 which is the latest official version.
Project Jackson Modules are pluggable components that extend out-of-box functionality to support additional data types or JVM languages. How to convert Java Map to / from JSON (Jackson) In this tutorial, we will show you few Jackson examples to convert JSON string to/from a Map. 1. JSON string to Map Example to convert JSON string to a Map. JsonMapExample.java Output 2. Example to convert the Map to JSON string. MapJsonExample.java package com.mkyong.json.example; import java.util.HashMap;import java.util.Map; import org.codehaus.jackson.map.ObjectMapper; public class MapJsonExample { public static void main(String[] args) { try { ObjectMapper mapper = new ObjectMapper(); String json = ""; Map<String, String> map = new HashMap<String, String>(); map.put("name", "mkyong"); map.put("age", "29"); //convert map to JSON string json = mapper.writeValueAsString(map); System.out.println(json); } catch (Exception e) { e.printStackTrace(); } }} 3.
Example to convert the Map to a JSON file. JsonMapFileExample.java c:\\user.json 4. Example to convert the JSON file to a Map. JsonFileMapExample.java. How to convert Java object to / from JSON (Jackson) In this tutorial, we show you how to use Jackson 1.x data binding to convert Java object to / from JSON. Note Jackson 1.x is a maintenance project, please use Jackson 2.x instead. 1. Quick Reference 1.1 Convert Java object to JSON, writeValue(...) ObjectMapper mapper = new ObjectMapper(); User user = new User(); mapper.writeValue(new File("c:\\user.json"), user); String jsonInString = mapper.writeValueAsString(user); 1.2 Convert JSON to Java object, readValue(...) P.S All examples are tested with Jackson 1.9.13 2.
For Jackson 1.x, it contains 6 separate jars for different purpose, in most cases, you just need jackson-mapper-asl. pom.xml <dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.13</version></dependency> 3. An User object for testing.
User.java package com.mkyong.json; import java.util.List; public class User { private String name; private int age; private List<String> messages; } 4. Convert an user object into a JSON formatted string. Jackson API.