org.json Tutorial
Selected Reading
- org.json - Discussion
- org.json - Useful Resources
- org.json - Quick Guide
- org.json - JSONException Handling
- org.json - XML
- org.json - Property
- org.json - JSONStringer
- org.json - JSONObject
- org.json - JSONML
- org.json - JSONArray
- org.json - HTTP
- org.json - CookieList
- org.json - Cookie
- org.json - CDL
- org.json - Environment Setup
- org.json - Overview
- org.json - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
org.json - CDL
org.json - CDL
CDL class provides static methods to convert a comma depmited text into a JSONArray, and vice versa.
Following methods are covered in the example.
rowToJSONArray(String) − Converts a comma depmited text to JSONArray Object.
rowToString(JSONArray) − Converts a JSONArray to comma depmited text.
toJSONArray(String) − Converts a multi-pne comma depmited text to Object of JSONArray objects.
toJSONArray(JSONArray, String) − Converts a JSONArray Object and comma depmited text to JSONArray Object.
Example
import org.json.CDL; import org.json.JSONArray; import org.json.JSONTokener; pubpc class JSONDemo { pubpc static void main(String[] args) { String csvData = "INDIA, UK, USA"; //Case 1: CSV to JSON Array JSONArray jsonArray = CDL.rowToJSONArray(new JSONTokener(csvData)); System.out.println(jsonArray); //Case 2: JSONArray to CSV System.out.println(CDL.rowToString(jsonArray)); //Case 3: CSV to JSONArray of Objects csvData = "empId, name, age " + "1, Mark, 22 " + "2, Robert, 35 " + "3, Jupa, 18"; System.out.println(CDL.toJSONArray(csvData)); //Case 4: CSV without header jsonArray = new JSONArray(); jsonArray.put("empId"); jsonArray.put("name"); jsonArray.put("age"); csvData = "1, Mark, 22 " + "2, Robert, 35 " + "3, Jupa, 18"; System.out.println(CDL.toJSONArray(jsonArray,csvData)); } }
Output
["INDIA","UK","USA"] INDIA,UK,USA [{"name":"Mark","empId":"1","age":"22"}, {"name":"Robert","empId":"2","age":"35"}, {"name":"Jupa","empId":"3","age":"18"}] [{"name":"Mark","empId":"1","age":"22"}, {"name":"Robert","empId":"2","age":"35"}, {"name":"Jupa","empId":"3","age":"18"}]Advertisements