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 - Encode JSONArray
JSON.simple - Encode JSONArray
Using JSON.simple, we can encode a JSON Array using following ways −
Encode a JSON Array - to String − Simple encoding.
Encode a JSON Array - Streaming − Output can be used for streaming.
Encode a JSON Array - Using List − Encoding by using the List.
Encode a JSON Array - Using List and Streaming − Encoding by using List and to stream.
Following example illustrates the above concepts.
Example
import java.io.IOException; import java.io.StringWriter; import java.util.LinkedList; import java.util.List; import org.json.simple.JSONArray; import org.json.simple.JSONValue; class JsonDemo { pubpc static void main(String[] args) throws IOException { JSONArray pst = new JSONArray(); String jsonText; pst.add("foo"); pst.add(new Integer(100)); pst.add(new Double(1000.21)); pst.add(new Boolean(true)); pst.add(null); jsonText = pst.toString(); System.out.println("Encode a JSON Array - to String"); System.out.print(jsonText); StringWriter out = new StringWriter(); pst.writeJSONString(out); jsonText = out.toString(); System.out.println(" Encode a JSON Array - Streaming"); System.out.print(jsonText); List pst1 = new LinkedList(); pst1.add("foo"); pst1.add(new Integer(100)); pst1.add(new Double(1000.21)); pst1.add(new Boolean(true)); pst1.add(null); jsonText = JSONValue.toJSONString(pst1); System.out.println(" Encode a JSON Array - Using List"); System.out.print(jsonText); out = new StringWriter(); JSONValue.writeJSONString(pst1, out); jsonText = out.toString(); System.out.println(" Encode a JSON Array - Using List and Stream"); System.out.print(jsonText); } }
Output
Encode a JSON Array - to String ["foo",100,1000.21,true,null] Encode a JSON Array - Streaming ["foo",100,1000.21,true,null] Encode a JSON Array - Using List ["foo",100,1000.21,true,null] Encode a JSON Array - Using List and Stream ["foo",100,1000.21,true,null]Advertisements