JSON.simple Tutorial
Selected Reading
- JSON.simple - Discussion
- JSON.simple - Useful Resources
- JSON.simple - Quick Guide
- Customized Output Streaming
- JSON.simple - Customized Output
- Primitive, Object, Map, List
- JSON.simple - Primitive, Map, List
- JSON.simple - Primitive, Object, Array
- JSON.simple - Merging Arrays
- JSON.simple - Merging Objects
- JSON.simple - Encode JSONArray
- JSON.simple - Encode JSONObject
- JSON.simple - Content Handler
- JSON.simple - Container Factory
- JSON.simple - Exception Handling
- JSON.simple - Using JSONValue
- Escaping Special Characters
- JSON.simple - JAVA Mapping
- JSON.simple - Environment Setup
- JSON.simple - Overview
- JSON.simple - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JSON.simple - Container Factory
JSON.simple - Container Factory
ContainerFactory can be used to create Custom container for parsed JSON objects/arrays. First we need to create a ContainerFactory object and then use it in parse Method of JSONParser to get the required object. See the example below −
Example
import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.json.simple.parser.ContainerFactory; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; class JsonDemo { pubpc static void main(String[] args) { JSONParser parser = new JSONParser(); String text = "{"first": 123, "second": [4, 5, 6], "third": 789}"; ContainerFactory containerFactory = new ContainerFactory() { @Override pubpc Map createObjectContainer() { return new LinkedHashMap<>(); } @Override pubpc List creatArrayContainer() { return new LinkedList<>(); } }; try { Map map = (Map)parser.parse(text, containerFactory); map.forEach((k,v)->System.out.println("Key : " + k + " Value : " + v)); } catch(ParseException pe) { System.out.println("position: " + pe.getPosition()); System.out.println(pe); } } }
Output
Key : first Value : 123 Key : second Value : [4, 5, 6] Key : third Value : 789Advertisements