Boon Tutorial
Selected Reading
- Boon - Discussion
- Boon - Useful Resources
- Boon - Quick Guide
- Boon - @JsonProperty
- Boon - @JsonViews
- Boon - @JsonInclude
- Boon - @JsonIgnore
- Boon - Generating Date
- Boon - String To Date
- Boon - Long To Date
- Boon - From Map
- Boon - From Object
- Boon - Sources
- Boon - To Map
- Boon - To Object
- Boon - Environment Setup
- Boon - Overview
- Boon - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Boon - From Map
Boon - From Map
ObjectMapper class can be used to generate a json string from a Map.
Example
Following example is using ObjectMapper class to generate a JSON string from a Map Object.
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.boon.json.JsonFactory; import org.boon.json.ObjectMapper; pubpc class BoonTester { pubpc static void main(String args[]){ ObjectMapper mapper = JsonFactory.create(); Map<String, String> student = new HashMap<>(); student.put("Name", "Mahesh"); student.put("RollNo", "21"); Map<String, String> student1 = new HashMap<>(); student1.put("Name", "Suresh"); student1.put("RollNo", "22"); List<Map<String,String>> studentList = new ArrayList<>(); studentList.add(student); studentList.add(student1); Map<String, List> studentMap = new HashMap<String, List>(); studentMap.put("students", studentList); String jsonString = mapper.writeValueAsString(studentMap); System.out.println(jsonString); } }
Output
When you execute the above code, you should see the following output −
{"students":[{"RollNo":"21","Name":"Mahesh"},{"RollNo":"22","Name":"Suresh"}]}Advertisements