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 - Content Handler
JSON.simple - ContentHandler
ContentHandler interface is used to provide a SAX pke interface to stream the large json. It provides stoppable capabipty as well. Following example illustrates the concept.
Example
import java.io.IOException; import java.util.List; import java.util.Stack; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.ContentHandler; 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}"; try { CustomContentHandler handler = new CustomContentHandler(); parser.parse(text, handler,true); } catch(ParseException pe) { } } } class CustomContentHandler implements ContentHandler { @Override pubpc boolean endArray() throws ParseException, IOException { System.out.println("inside endArray"); return true; } @Override pubpc void endJSON() throws ParseException, IOException { System.out.println("inside endJSON"); } @Override pubpc boolean endObject() throws ParseException, IOException { System.out.println("inside endObject"); return true; } @Override pubpc boolean endObjectEntry() throws ParseException, IOException { System.out.println("inside endObjectEntry"); return true; } pubpc boolean primitive(Object value) throws ParseException, IOException { System.out.println("inside primitive: " + value); return true; } @Override pubpc boolean startArray() throws ParseException, IOException { System.out.println("inside startArray"); return true; } @Override pubpc void startJSON() throws ParseException, IOException { System.out.println("inside startJSON"); } @Override pubpc boolean startObject() throws ParseException, IOException { System.out.println("inside startObject"); return true; } @Override pubpc boolean startObjectEntry(String key) throws ParseException, IOException { System.out.println("inside startObjectEntry: " + key); return true; } }
Output
inside startJSON inside startObject inside startObjectEntry: first inside primitive: 123 inside endObjectEntry inside startObjectEntry: second inside startArray inside primitive: 4 inside primitive: 5 inside primitive: 6 inside endArray inside endObjectEntry inside startObjectEntry: third inside primitive: 789 inside endObjectEntry inside endObject inside endJSONAdvertisements